Why does my :MoveTo() become lazy?

So I was making a tds game and I added pathing to my enemies but then they desided to skip some of the parts of the waypoints. Here is some proof
robloxapp-20211219-2131155.wmv (2.5 MB)

Here is my path script:

function mob.Move(enemy, map)
	local humanoid = enemy.Humanoid 
	local waypoints = map.WayPoints

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

Does anyone know how to fix this issue?

2 Likes

If I where you in this senerio, I wouldn’t do that. I would do it in ipairs instead.

function mob.Move(enemy, map)
	local humanoid = enemy.Humanoid 
	local waypoints = map.WayPoints

	for i, waypoint in ipairs(waypoints:GetChildren()) do
		humanoid:MoveTo(waypoint.Position)
        print("Moving to "..waypoint)
		humanoid.MoveToFinished:Wait()

	end
end

You could also use pairs if that’s easier

It stops mid way through. what should I do?

It may be an issue with humanoid.MoveToFinished:Wait(). I would try changing it to wait(5) first to see if that is the issue, but doing so will obviously not be a permanent solution.

They turn each second and then continue till the end

Here should be the full script:

local serverStorage = game:GetService("ServerStorage")
local mob = {}

function mob.Move(enemy, map)
	local humanoid = enemy.Humanoid 
	local waypoints = map.WayPoints

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

function mob.Spawn(enemyTy, quantity, sDelay, map)
	for i=1, quantity do
		local enemyExists = serverStorage.Enemies:FindFirstChild(enemyTy)

		if enemyExists then
			task.wait(sDelay)
			local newEnemy = enemyExists:Clone()
			newEnemy.HumanoidRootPart.CFrame = map.Start.CFrame
			newEnemy.Parent = workspace.Enemies

			coroutine.wrap(mob.Move)(newEnemy, map)
		else
			warn("There is no existing:"..enemyTy.."(Error: Enemy does not exist)")
		end	
	end

end

return mob

Use HumanoidRootPart, it does the same thing, yet it can be more reliable in instances or set the Primary CFrame of the object(s) to a position.

You should probably switch from MoveToFinished to a Heartbeat repeat loop.

repeat
   game:GetService("RunService").Heartbeat:Wait()
until (enemy.HumanoidRootPart.Position - waypoints[waypoint].Position).Magnitude <= 5

This works with magnitude, so I recommend putting the waypoints as close as possible to the ground so the Y axis doesn’t affect it. There’s an easy way to fix it but you can just put the waypoints on the ground instead.

How would I come to using it? Like HumanoidRootPart:MoveTo()

Now this happens:


And I did put the waypoints as close to the ground

They don’t move at all? -------

They move but then they stop for some unknown reason. There is still no errors in the console.

I’d try changing the direction they’re moving to inside the repeat loop, this way you can make sure they will always try to go towards the waypoint.

repeat
   game:GetService("RunService").Heartbeat:Wait()
   humanoid:MoveTo(waypoints[waypoint].Position)
until (enemy.HumanoidRootPart.Position - waypoints[waypoint].Position).Magnitude <= 5

They now skip differently:
robloxapp-20211220-1857142.wmv (2.7 MB)
But I still dosen’t work

Edit: I will try mess around with magnitude

Lower the amount of magnitude. Instead of 5 put, I don’t know, 2 or 3?
Also another tip I can give is to add invisible walls around the paths so they cannot get out of them.

Would decimal magnitude values work because it nearly works

Yes you can use decimals if you want.

completely unrelated question if mods won’t remove this: what’s the difference between ipairs and pairs?

I am just going to report this as a bug as no one can fix this :confused:

It’s not a bug. Just an issue with ur script.