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

How to check when Humanoid arrives at destination set by MoveTo?

EDIT: Fixed, see below for the lines that fixed my script:

local speed = npc.Humanoid.Running:wait()
if speed ~= 0 then repeat speed = npc.Humanoid.Running:wait() until speed == 0 end

Hi guys, I was wondering how I could check if a Humanoid arrives at a WalkToPoint set by the MoveTo function. I need this so I can begin re-moving my NPC when they arrive at their destination to give a sense of movement.

I have tried making it so it waits until the torso's X position equals the destination's X position, but this doesn't work as the Humanoid can sometimes overshoot it and be a few decimal places away from it.

Here is my current script, without waiting for the Humanoid to arrive at their destination:

local region = require(game.ServerScriptService:WaitForChild("RegionScript")) --ModuleScript that generates a random position when RandomiseLocation is called.
local paths = game.Lighting.Paths:GetChildren()
local npc = script.Parent
npc:WaitForChild("Humanoid")
npc:WaitForChild("Torso")

while true do
	wait(2)
	local rand = math.random(1,#paths)
	local randLoc = region:RandomiseLocation(paths[rand])
	print(randLoc.X, randLoc.Y, randLoc.Z)
	npc.Humanoid:MoveTo(randLoc)
end

Any help is appreciated, thanks!

3 comments

1

http://wiki.roblox.com/index.php?title=Running they stop walking when the speed is 0.

0

Wow, can't believe it was that simple. Thanks!

0

I turned it into an answer form, you can accept it

2 answers

The MoveToFinished event of Humanoid fires when a MoveTo function is, well, finished.

Exactly what you want(:

Usage;

local region = require(game.ServerScriptService:WaitForChild("RegionScript"))
local paths = game.Lighting.Paths:GetChildren()
local npc = script.Parent
npc:WaitForChild("Humanoid")
npc:WaitForChild("Torso")

while wait(2) do
	local rand = math.random(1,#paths)
	local randLoc = region:RandomiseLocation(paths[rand])
	print(randLoc.X, randLoc.Y, randLoc.Z)
	npc.Humanoid:MoveTo(randLoc)
	--Wait until it's finished
	npc.Humanoid.MoveToFinished:wait()
	print'MoveTo finished'
end

3 comments

0

Dang it, you always need to 1up me XD

0

<3 c:

0

I upvoted yours. Yours is good too just not as efficient.

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