Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local npc = game.Workspace.NPC -- change NPC to nickname
- local players = game.Players:GetPlayers()
- local function findNearestPlayer()
- local minDist = math.huge
- local nearestPlayer = nil
- for _, player in pairs(players) do
- local dist = (npc.Position - player.Character.HumanoidRootPart.Position).magnitude
- if dist < minDist then
- minDist = dist
- nearestPlayer = player
- end
- end
- return nearestPlayer
- end
- local function moveToNearestPlayer()
- local nearestPlayer = findNearestPlayer()
- if nearestPlayer then
- local path = game:GetService("PathfindingService"):CreatePath({
- AgentRadius = 2,
- AgentHeight = 5,
- AgentCanJump = true,
- AgentJumpHeight = 10
- })
- path:ComputeAsync(npc.Position, nearestPlayer.Character.HumanoidRootPart.Position)
- path:MoveTo(npc)
- end
- end
- while true do
- moveToNearestPlayer()
- wait(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement