Hello! I had this issue where my NPC didn’t quite work for this one time, for my game that I have tested multiple times, but it seems to me like the NPC broke on the last time probably caused my MoveTo Script. I have this MoveTo Script that controls the main walking function for this NPC, but I don’t have a way of preventing any errors if the NPC for what ever reason doesn’t make it to its destination.
What i’m hoping to achieve is that even if the NPC breaks for whatever reason or if it doesn’t reaches its destination, it cancels its current function and waits for its next instructions to be called, this is done with my changed event value. If anyone could help me out here I’d appreciate it! Here is the code for it:
events.WalkToValue.Changed:Connect(function()
if events.WalkToValue.Value == ("Walking1") then
for i = 1, #parts do
AlignOrientation.Enabled = false
AlignPosition.Enabled = false
repeat
wait()
script.Parent.Humanoid:MoveTo(coordinates.WalkFolder1[i].Position)
until (script.Parent.HumanoidRootPart.Position - coordinates.WalkFolder1[i].Position).magnitude < 5
end
AlignOrientation.Enabled = true
AlignPosition.Enabled = true
end
end)
Humanoids have an event called MoveToFinished. This will fire once the player reaches its destination, or after 8 seconds. You can replace your repeat loop with this:
script.Parent.Humanoid:MoveTo(coordinates.WalkFolder1[i].Position)
local ReachedGoal = script.Parent.Humanoid.MoveToFinished:Wait()
This piece of code will fire MoveTo and then wait until MoveToFinished is recieved. Once MoveToFinished fires you also get a boolean, in this case called ReachedGoal, which indicates if the character reached its goal or not. If it returns false (meaning it timed out) you can simply teleport the character to the end position and return the function.
The only limitation with this is that the timeout is locked at 8 seconds, which is truly a bummer.
The first thing you should do is hold the Humanoid in a variable so you don’t have to go back through the hierarchy over again. This also helps in the instance that the parent changes so that you still have a reference to the humanoid you need.
local Humanoid = script.Parent.Humanoid
As for accessing the boolean, @0skarian did it wrongly; the wait method on any RBXScriptConnection does not pass any values. To get the boolean returned, you simply need to assign the value of RBXScriptConnection.Wait to a variable.
local reachedGoal = Humanoid.MoveToFinished:Wait()
-- Example handling chunk via if statement
if reachedGoal then
-- Your code
end