[code
local PathfindingService = game:GetService(“PathfindingService”)
local function moveNPCToTarget(npc, targetPart)
if not npc or not targetPart then return end
local humanoid = npc:FindFirstChildOfClass("Humanoid")
local rootPart = npc:FindFirstChild("HumanoidRootPart")
if not humanoid or not rootPart then return end
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 7,
AgentWalkableFloorAngle = 60
})
path:ComputeAsync(rootPart.Position, targetPart.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
warn("Pathfinding failed!")
task.wait(1)
moveNPCToTarget(npc, targetPart)
return
end
end
[/code]
So I’m calling this function in a while loop every millisecond. It loops maybe 60 times before it breaks.
I’ve determined the script is getting hung up on this part humanoid.MoveToFinished:Wait()
It only resumes once the npc reaches that specific waypoint. But I need that not to be the case. Because if the player moves, the pathfind doesn’t catch up with the updated positions.
Any ideas how to fix this? Or a better pathfinding system?