:MoveToFinished() triggers way too early

I’m making a game, and in the game there’s an npc who moves by 7 different positions in a order, for some reason, the :MoveToFinished() i added fires less than halfway through the way, my npc’s walkspeed is 2, when i set the enemy’s walkspeed to 16, it does it just fine, how can i fix this?

My script:

local zombie = script.Parent

for count = 1,#game.Workspace.WayPoints:GetChildren(),1 do
	wait(0.1)
	zombie.Humanoid:MoveTo(workspace.WayPoints:FindFirstChild(tostring(count)).Position)
	zombie.Humanoid.MoveToFinished:Wait()
	wait(0.05)
end

wait(0.6)
game.ReplicatedStorage.HP.Value = game.ReplicatedStorage.HP.Value - script.Parent.Humanoid.Health
zombie:Destroy()

EDIT: I just found out that it’s actually the :MoveTo() that for some reason ends early before reaching the wanted point.
EDIT 2: Setting WalkToPoint gives the same result, and setting WalkToPart does absolutely nothing.

I’m no expert but I think that’s a Roblox problem. But a solution might be to add a yield like you are doing but scale the yield according to the WalkSpeed.

[EDIT]
Something like

wait(16/zombie.Humanoid.WalkSpeed*0.05)

maybe.

1 Like

How would i do that? I don’t really know what you mean

Something else you might be able to do is remove the :MoveToFinished:Wait() and simply divide the distance by the speed and yield that amount.

local dist = (zombie.Humanoid.WalkToPoint - zombie.HumanoidRootPart.Position).Magnitude -- the distance
wait(dist/zombie.Humanoid.WalkSpeed)

Because it theoretically takes distance/speed seconds to go from point A to point B.

1 Like

I just found this out right now and i’m trying to find a way to fix it.

Found this while trying to fix it.

1 Like

So I tested your code and I think I know what’s happening. There is a “timeout” with the :MoveTo() function which is 8 seconds. If the npc cannot move to the position or part within 8 seconds it will give up.
But on Humanoid:MoveTo the code sample is really good and I would recommend using it to fix your problem.

2 Likes

MoveTo() times out after 8 seconds, resulting in MoveToFinished firing regardless of whether or not the humanoid reached its specified destination.

https://developer.roblox.com/en-us/api-reference/event/Humanoid/MoveToFinished

Use MoveToFinished's parameter to determine if the goal was reached.

2 Likes

Thanks, the code sample helped and fixed the issue.