(SOLVED) Humanoid:MoveTo not working

I’m trying to make a group of NPCs walk to 4 points in a succession, but they seem to give up partway to the 4th point, anyone have any idea why?

The 4th point is very far away compared to the other 3, I wondered if maybe humanoids simply give up after walking for long enough?

Here’s my script:

local npc = script.Parent
local humanoid = npc.Humanoid
local waypoints = game.Workspace.Waypoints


        game.ReplicatedStorage.Attack.Event:Connect(function()	
	wait(0.1)
		humanoid:MoveTo(waypoints.W1.Position)
		
		humanoid.MoveToFinished:Wait()
	wait(0.1)
		humanoid:MoveTo(waypoints.W2.Position)
		
		humanoid.MoveToFinished:Wait()
	wait(0.1)
		humanoid:MoveTo(waypoints.W3.Position)
		
	    humanoid.MoveToFinished:Wait()
	wait(0.1)
	    humanoid:MoveTo(waypoints.W4.Position)
	print("success")
end)


The red spheres circled in red are the waypoints and the little orange guy circled in green (might be kinda hard to see) is the NPC where he stops every time, roughly 1 3rd or 1 4th of the way there

(P.S. I don’t believe there are any errors)

Any and all help is appreciated, thanks in advance!

1 Like

Not the best script I’ve ever seen, but you should be using a for loop.

Here’s your new script, I’m happy to explain anything if you don’t understand. (I’ve kept it in the original state of predetermined waypoints in case you need it like this)

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local waypoints = game.Workspace:WaitForChild("Waypoints")

local function moveToWaypoint(waypoint)
    humanoid:MoveTo(waypoint.Position)
    humanoid.MoveToFinished:Wait()
    wait(0.1)
end

game.ReplicatedStorage.Attack.Event:Connect(function()
    moveToWaypoint(waypoints.W1)
    moveToWaypoint(waypoints.W2)
    moveToWaypoint(waypoints.W3)
    moveToWaypoint(waypoints.W4)

    print("Movement Finished!")
end)
1 Like

:MoveTo has a default timeout of 8 seconds so this could be the cause of your problem.
There is sample code in the Roblox docs to remove the timeout:

2 Likes

It says:
" The reach goal state of a humanoid will timeout after 8 seconds if it doesn’t reach its goal. This is done so that NPCs won’t get stuck waiting for Humanoid.MoveToFinished to fire. If you don’t want this to happen, you should repeatedly call MoveTo so that the timeout will keep resetting."
This seems like it may be the issue but I don’t know how to repeatedly call MoveTo

You could just use the function in the sample code from the documentation so your code would look something like this:

local npc = script.Parent
local humanoid = npc.Humanoid
local waypoints = game.Workspace.Waypoints
local waypointList={waypoints.W1, waypoints.W2, waypoints.W3, waypoints.W4}
-- I've created a table of your waypoints to make the code easier to read.  You can simply add more waypoints to the list if required.

local function moveTo(humanoid, targetPoint) -- This is the function from the sample code
	local targetReached = false

	-- listen for the humanoid reaching its target
	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
		
	end)

	-- start walking
	humanoid:MoveTo(targetPoint)

	-- execute on a new thread so as to not yield function
	task.spawn(function()
		while not targetReached do
			-- does the humanoid still exist?
			if not (humanoid and humanoid.Parent) then
				break
			end
			-- has the target changed?
			if humanoid.WalkToPoint ~= targetPoint then
				break
			end
			-- refresh the timeout
			humanoid:MoveTo(targetPoint)
			task.wait(6)
		end

		-- disconnect the connection if it is still connected
		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

game.ReplicatedStorage.Attack.Event:Connect(function()
	
	for _, wp in ipairs(waypointList) do	 -- loop through the waypoint list
		task.wait(0.1)
		moveTo(humanoid, wp.Position) -- using the custom moveTo function that doesn't timeout 
        humanoid.MoveToFinished:Wait()
	end
	print("success")
end)
1 Like

Worked flawlessly, thanks so much!

Edit: works almost flawlessly but upon further inspection they seem to go straight to the 4th skipping 1 2 & 3, any idea why?

1 Like

Ahh my bad - forgot to include humanoid.MoveToFinished:Wait() after calling moveTo so edit your code:

game.ReplicatedStorage.Attack.Event:Connect(function()
	
	for _, wp in ipairs(waypointList) do	 -- loop through the waypoint list
		task.wait(0.1)
		moveTo(humanoid, wp.Position) -- using the custom moveTo function that doesn't timeout 
        humanoid.MoveToFinished:Wait()
	end
	print("success")
end)

Huh? I’m afraid I don’t follow

(nevermind I’m stupid :man_facepalming:)

1 Like

Just add this line of code to your exising code. Put it directly after moveTo(humanoid, wp.Position) :

2 Likes

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