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
end
Humanoid:MoveTo(v.Position)
local timeOut = Humanoid.MoveToFinished:Wait(1)
if not timeOut then
Humanoid.Jump = true
print("path too long")
end
if not (target.Position == targetPosition) then
print("target has moved")
findPath(target)
break
end
end
end
end
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.