Trying to make an NPC that uses pathfinding to follow a player, but whenever I test it the NPC stutters and lacks behind. Any help?
local workspace = game.Workspace function moveToPostition(moveTo) local path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200) local points = path:GetPointCoordinates() for p = 1, #points do script.Parent:MoveTo(points[p]) repeat wait() until script.Parent.MoveToFinished end end while true do wait(0.01) walkevent = moveToPostition(workspace.BoxedHouses.Torso.Position) end
Alright, not sure if I'm still misunderstanding the problem somewhat, but let me know if the following works:
local pathfindingCoolDown = 0.2; function moveToPosition(moveTo) local path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200) local points = path:GetPointCoordinates() local startTime = tick(); for p = 1, #points do script.Parent:MoveTo(points[p]) script.Parent.MoveToFinished:Wait(); if(startTime-tick() > pathfindingCoolDown) then return end end end while wait() do moveToPosition(workspace.BoxedHouses.Torso.Position) end