MoveTo() Doesn't Fully Move To Destination

Hi, I’m creating an NPC System (Move from one node to another)
But the NPC doesn’t always reach the destination.

I am changing the Animation/Walk speed from node to node

As you can see in the image above the NPC moves to 1-2-3-4 correctly but at 5th he stops before reaching the end and continue the loop normally

while 1 do
	for i,v in PathNodes do
		
		if Animation ~= nil then Animation:Stop() end
		Animation = Humanoid:LoadAnimation(SetSpeedAndGetAnimation(v["WalkFromHere"].Value))
		Animation:Play()
		Humanoid:MoveTo(v.Position)
		Humanoid.MoveToFinished:Wait()
		Animation:Stop()
		Animation = Humanoid:LoadAnimation(IdleAnimation)
		Animation:Play()
		task.wait(v["WaitTime"].Value)
	end
end

PathNodes are those red parts in the Image above

that SetSpeedAndGetAnimation() is where I set the humanoid speed and returns the correct animation.

Is there any limitations to the MoveTo() function?
how can I fix this? any help would be appreciated :slight_smile:

2 Likes

After 7 or 8 seconds, the MoveTo function stops moving the humanoid because it thinks it failed to get to the destination. Is this the issue?

The NPC doesn’t get stuck anywhere but yeah he takes about 8 seconds before stopping

Replace that with:

repeat until Humanoid.MoveToFinished:Wait()

Because, MoveToFinished returns if the humanoid reached the destination.

Still no luck, now he’s getting stuck at that same point and ofc doesn’t continue the loop
I even moved the nodepart to somewhere else

When you moved the node to ‘somewhere else’ did you move it closer to 4 to see if that was the issue?

If I move it closer to the 4 , it works perfectly but is there anything that I can do to fix this issue?
Or anyway that I can detect the fail

The documentation used to say that if your NPC has not reached its MoveToFinished within that 8 seconds, to call another MoveTo function on the NPC and this will reset the 8 seconds… ofcourse a hacky method is to add an invisible waypoint between 4 and 5 to circumvent this.

So basically a loop of MoveTos that repeats every 7 seconds?

1 Like

You can check for the NPC reaching its MoveToFinished, and if it hasn’t then you call another MoveTo yes… its always been a pathfinding issue, and the easiest method is just to create invisible waypoints that are close together so that this 8 second limit isn’t reached. This is why i’ve always struggled with pathfinding, it’s too bad you can’t adjust the time restriction (not that i know of atleast).

Guess there is no easy way to fix this :frowning:

To fix these issues, you can try the following:

  1. Check the path nodes to make sure they are in clear, accessible areas.
  2. Try adjusting the position of the destination point slightly to see if that helps.
  3. Increase the WalkSpeed of the humanoid to get to the destination faster.
  4. You can also try using the PathfindingService and Path class to generate a more accurate path to the destination. This would involve creating a Path object between the NPC’s current position and the destination, and then using the MoveToPart() function to move the humanoid along the path

1.They Are
2.If they are closer, they works perfectly
3.want to them to be able to walk/run so different speeds
4.I have no experience with “PathfindingService” but i can research it bit wont MoveToPart() Have the same exact 8 second limitatiion?

I’ve Implemented this and it works finally!

function MoveNPC(v)
	Humanoid:MoveTo(v.Position)
	Humanoid.MoveToFinished:Wait()
	local Distance = (RootPart.Position - v.Position).Magnitude
	if(Distance < 2) then
		return
	else
		return MoveNPC(v)
	end
end

while 1 do
	for i,v in PathNodes do
		
		if Animation ~= nil then Animation:Stop() end
		Animation = Humanoid:LoadAnimation(SetSpeedAndGetAnimation(v["WalkFromHere"].Value))
		Animation:Play()
		MoveNPC(v)
		Animation:Stop()
		Animation = Humanoid:LoadAnimation(IdleAnimation)
		Animation:Play()
		task.wait(v["WaitTime"].Value)
	end
end

all I’m checking is the distance between player and node once the move finished/failed

2 Likes

Unrelated but you should use Animator instead. Loading animations on humanoid is deprecated.

		Animation = Humanoid.Animator:LoadAnimation(SetSpeedAndGetAnimation(v["WalkFromHere"].Value))

I will do that, thanks for telling

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.