Hello developers,
Im currently making an npc movie script and the move parts are pretty distanced apart from each other. I use :MoveTo for the npc so it can reach the destinations that i would like it to. But i found out that humanoid.MoveToFinished:Wait() only stops if: it has reached the destination or it has been waiting for 8 seconds and has not reached the destination. Since like i said, my destinations are far apart and the npcs are relatively slow, how would i repeat the current line on my for,i loop until it has ACTUALLY reached the destination.
Here is the script, im basically asking on how to repeat the current line on the for i loop until it has actually reached the destination:
for waypoint=1, #chosenpoints:GetChildren() do
humanoid:MoveTo(chosenpoints[waypoint].Position)
humanoid.MoveToFinished:Wait()
-- i want to check if its reached there and if it hasnt it tries again without going onto the next line in the for i loop.
end
maybe just keep calling it? after it finished, check if it is at the spot, if not, move to again
Yeah but im asking on how to do that. i would probably use MoveToFinished:Connect(function()) to check if it has reached the destination but how would i go to the next line in the for i loop if it has and how would i not if it hasnt reached it.
there is a example script in the documentation
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(reached)
end
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
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
I would personally just use a promise module for this.