Problem with NPC tracking script

The script is meant to help the NPC track down the nearest player. However, the problem is that the NPC doesn’t move when the closest player is constantly moving. For some reason, only when the closest player stands still, can the NPC continue moving. How can this be fixed?

function findPath(target)
	local targetPosition = target.Position
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(myTorso.Position, targetPosition)
	local waypoints = path:GetWaypoints()
	if path.Status == Enum.PathStatus.Success then
		print("target locked")
		for i, v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				Humanoid.Jump = true
			local timeOut = Humanoid.MoveToFinished:Wait(1)
			if not timeOut then
				Humanoid.Jump = true
				print("path too long")
			if not (target.Position == targetPosition) then
				print("target has moved")

I think the problem lies within the if not (target.Position == targetPosition then section of the code, but I have no idea how to solve it.

I’m guessing this had something to do with the generation of waypoints. The first waypoint is always where the NPC is standing. Perhaps the NPC is constantly trying to MoveTo() the first waypoint, then fires MoveToFinished immediately.

To mitigate, replace the ipairs loop with a regular index loop, the starting index being 2, and the last index being #waypoints:

for index = 2, #waypoints do
	local currentWaypoint = waypoints[index]