Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I Change the MoveTo() Timeout?

I'm using the MoveTo() function on a humanoid to move it on a path, from one point to another along the path. However the timeout frequently makes the humanoid move on to the next point instead of going where it's supposed to. Here's my code:

Spawned.Humanoid:MoveTo(d1.Position)
Spawned.Humanoid.MoveToFinished:wait()		
Spawned.Humanoid:MoveTo(d2.Position)	
Spawned.Humanoid.MoveToFinished:wait()
Spawned.Humanoid:MoveTo(d3.Position)
Spawned.Humanoid.MoveToFinished:wait()
Spawned.Humanoid:MoveTo(d4.Position)
Spawned.Humanoid.MoveToFinished:wait()
Spawned.Humanoid:MoveTo(d5.Position)
Spawned.Humanoid.MoveToFinished:wait()	
Spawned.Humanoid:MoveTo(d6.Position)
Spawned.Humanoid.MoveToFinished:wait()
Spawned.Humanoid:MoveTo(d7.Position)
Spawned.Humanoid.MoveToFinished:wait()

Spawned is the variable for the rig, and I established d1-d7 as variables as well. When this is run the default timeout of 8 seconds causes the humanoid to go to the next spot before it has gone to the current one, therefore not completing the path as it should. Is there a way to change the timeout, and if there isn't how do I make it work correctly? I am very new to this function, so I apologize if this is a dumb question. Thank you for reading!

2 answers

✓ Accepted answer

Check this place out. At the bottom theres a script that mentions using MoveTo with no time out.

https://developer.roblox.com/en-us/api-reference/function/Humanoid/MoveTo

local function moveTo(humanoid, targetPoint, andThen)
	local targetReached = false
 
	-- listen for the humanoid reaching its target
	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
		if andThen then
			andThen()
		end
	end)
 
	-- start walking
	humanoid:MoveTo(targetPoint)
 
	-- execute on a new thread so as to not yield function
	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)
			wait(6)
		end
		
		-- disconnect the connection if it is still connected
		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

I recommend using something else instead of spamming MoveTo called PathfindingService. This is probably what you should look into as it auto generates a path for you which will make your life easier with NPCs. Here are the docs for: PathfindingService

1 comment

0

This creates a path from one place to another without touching a wall or other part, I don’t think this will be useful to move a humanoid along a path I made

This discussion is preserved as it was. New answers, comments, and votes are closed.