# Npc's moving to next waypoint before getting to the current waypoint

1. What do you want to achieve? Keep it simple and clear!
Im trying to getting all the enemy npc’s to get to their current waypoint before moving to the next waypoint

2. What is the issue? Include screenshots / videos if possible!
video of what is happening - GnomeCode TD tutorial glitch - YouTube -

the blue squares are waypoints and the red square is where they spawn

1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried looking this up but it seems to specific for me to find anything on it.

This is the code i use to move the npcs

``````function mob.Move(mob, map)
local hum = mob:WaitForChild("Humanoid")
local waypoints = map.Waypoints

for waypoint=1, #waypoints:GetChildren() do
hum:MoveTo(waypoints[waypoint].Position)
hum.MoveToFinished:Wait()
end

mob:Destroy()
end

return mob

``````

This started happening after i changed the walkspeed of the enemies but i dont think that would affect anything

1 Like

MoveToFinished has an 8 second timeout… so if it will take longer than that than call another MoveTo before the MoveToFinished so that it refreshes the function.

Because of the timeout with Humanoid:MoveTo(), it would be more effective to check distance from the waypoint rather than wait for MoveToFinished.

``````for waypoint=1, #waypoints:GetChildren() do
repeat
hum:MoveTo(waypoints[waypoint].Position)
wait()
--adjust the 0.2 to increase/decrease the threshold to move to next waypoint
until ((mob.RootPart.Position - waypoints[waypoint].Position) * Vector3.new(1,0,1)).Magnitude <= 0.2
end
``````
1 Like

why do you multiply a vector3 against the value given from subtracting the humanoidrootpart position and waypoint position?

This is done to get the distance. I recommend changing `WaypointSpacing` instead.

Multiplying the Vector3 by (1,0,1) means that it’s only going to get the Distance between them horizontally, not vertically. It effectively takes the Y component out of the Distance calculation, so the Height of your NPCs won’t make a difference.

You should check if the function gets fired more than once in the same second with a print. It’s probably not the case but I did have this issue before.

A bit late of a response, OP isn’t using PathfindingService, they’re using predefined waypoints.

Humanoid:MoveTo are bad for making a TD. I used CFrame and heartbeat to move the enemy. It’s complicated but worth it