Problems with :MoveTo()

Hello everybody!
I’m once again asking you guys for help with :MoveTo().

So, in my tower defense game, zombies spawn at the start of the map and make their way to the end by walking from a waypoint to another.
Except the fact that some zombies do that, while other get stuck.

The issue:
Some of the zombies can walk between waypoints and get to the end, but bigger enemies (bosses, mini-bosses) tend to get stuck at the first one. Here’s a video showing the problem:

The code I used:
This is the code that is responsible for the pathfinding (every zombie’s movement is controlled by a main script)

for _, waypoints in pairs(script.Parent.Waypoints:GetChildren()) do
	
	local successStatus = false
	
	local function timeoutUpdater()
		spawn(function()
			wait(7.8)
			if successStatus == false then
				if zombie:FindFirstChild("Humanoid") then
					zombie.Humanoid:MoveTo(waypoints.Position)
					timeoutUpdater()
				end
			end
		end)						
	end
	
	timeoutUpdater()
	
	zombie.Humanoid:MoveTo(waypoints.Position)
	zombie.Humanoid.MoveToFinished:Wait()
	
	successStatus = true
	
end

The solutions I tried:
For now, I only tried changing the :MoveTo() position from waypoints.Position to a Vector3.new having as parameters waypoints.Position.X, zombie.HumanoidRootPart.Position.Y, waypoints.Position.z. Example in the code:

for _, waypoints in pairs(script.Parent.Waypoints:GetChildren()) do
	
	local successStatus = false
	
	local moveToPos = Vector3.new(waypoints.Position.X, zombie.HumanoidRootPart.Position.Y, waypoints.Position.z)
	
	local function timeoutUpdater()
		spawn(function()
			wait(7.8)
			if successStatus == false then
				if zombie:FindFirstChild("Humanoid") then
					zombie.Humanoid:MoveTo(moveToPos)
					timeoutUpdater()
				end
			end
		end)						
	end
	
	timeoutUpdater()
	
	zombie.Humanoid:MoveTo(moveToPos)
	zombie.Humanoid.MoveToFinished:Wait()
	
	successStatus = true
	
end

Other notes:
I don’t really know much about making humanoids move / pathfinding so sorry if it’s something really obvious.

Edit: Forgot to mention, CanCollide is off for al the waypoints, but it should be obvious as the smaller zombies go through them.

1 Like

Can you send the whole script if possible? Cuz right now it looks like you are getting the waypoints from a folder and not calculating them with PathfindingService like you are claiming you do

Talking about this line here

This is the whole script. I don’ use PathfindingService to calculate the path as the zombies have to walk in a straight line and calculating the path would cause problems and lag (before this, I actually calctulated paths but it rarely worked correctly). Instead, I use :MoveTo().

Reading your response and this made me noticed I don’t actually use pathfinding, so it’s better if I change the title and some of the text. Thanks for making me notice.

I recommend changing the title and some of the text yes to avoid confusion for others.

Your script in that case looks fine and it should work on theory. Are there no errors popping up?

Alright i made some slight changes to your code, i kinda changed the way you loop through the waypoints.

To make this work you have to name all your waypoints in the order they go. (So the first waypoint they have to go to is obviously called “1” and the next one “2”)

local zombie = script.Parent
local Waypoints = script.Parent.Waypoints
for index = 1, #Waypoints:GetChildren() do
	local waypoint = Waypoints:FindFirstChild(index)
	local successStatus = false

	local moveToPos = Vector3.new(waypoint.Position.X, zombie.HumanoidRootPart.Position.Y, waypoint.Position.z)

	local function timeoutUpdater()
		spawn(function()
			wait(7.8)
			if successStatus == false then
				if zombie:FindFirstChild("Humanoid") then
					zombie.Humanoid:MoveTo(moveToPos)
					timeoutUpdater()
				end
			end
		end)						
	end

	timeoutUpdater()

	zombie.Humanoid:MoveTo(moveToPos)
	zombie.Humanoid.MoveToFinished:Wait()

	successStatus = true
end

Let me know if this solves your problem :slight_smile:

The output was completely empty in both the normal and modified script (with modified I mean the solution I tried)

Thanks a lot for the code, I think I’m going to have a lot less problems with placing waypoints now (I had to guess which one the zombies woul go to first up until now, so that’s a big change), but the bosses keep getting stuck. Honestly, now I’m concerned it might have something to do with the humanoid HipHeight, though I’m not really sure if that matters with :MoveTo().

Update: Nope. It wasn’t the HipHeight.

MoveToFinished Automatically fires after 8 seconds even when the destination has not been reached. MoveToFinished when fired returns an boolean telling if it has reached the destination. If false, call MoveTo again.

I already know that. In fact, the function timeoutUpdater() is made to keep enemies moving.

Might be this issue:

I’ll give it a check and let you know.

Since the big guy is walking slower, could this be the problem(an 8 second timeout):

using the Humanoid:MoveTo function.

If the Humanoid reaches its goal within 8 seconds, this event will return with reached as true. If the goal is not reached within 8 seconds the Humanoid will stop walking and reached will be false

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

If so, there is an example script on that page that tells how to bypass the timeout by calling MoveTo every 6 seconds.

You need to repeatedly call moveto every couple seconds, otherwise it stops after 8 seconds of not reaching the target. You also need to make sure to call moveto in a sereverscript, otherwise it won’t work correctly

1 Like