Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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!

1
http://wiki.roblox.com/index.php?title=Running they stop walking when the speed is 0. EzraNehemiah_TF2 3552 — 9y
0
Wow, can't believe it was that simple. Thanks! Spongocardo 1991 — 9y
0
I turned it into an answer form, you can accept it EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

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

Ad
Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
0
Dang it, you always need to 1up me XD EzraNehemiah_TF2 3552 — 9y
0
<3 c: Goulstem 8144 — 9y
0
I upvoted yours. Yours is good too just not as efficient. Goulstem 8144 — 9y

Answer this question