Issue with my pathfinding script

[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?

Why cant you just remove the moveToFinished:wait()? You could also just use this pathfinding module: SimplePath - Pathfinding Module

If I remove that, then he never reaches the waypoint aka never follows the path. Remember that I’m calling this function every millisecond.

It should still move–even if you binded it to heartbeat, since it still calls the moveto() function.

i dont think roblox has a variable called “AgentWalkableFloorAngle = 60” so that might be the issue also sadly roblox pathfinding dont have agent jump height. it has a bool CanJump

unfortunately, default Roblox pathfinding is horrible for following targets. You would need to make an entire pathfinding AI from scratch or just search up a pathfinding algorithm that actually follows targets, if you still can’t get this to work I guess the only thing you could do is just remove the pathfinding entirely and just make the humanoid always move towards the player, but that would mean it would be easily stuck