You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I want an NPC to move to a part.
What is the issue?Humanoid.MoveToFinished fires before the NPC actually reaches the part.
Here is the code I use:
local zombie = script.Parent
local humanoid = zombie.Humanoid
local point = workspace.Point
humanoid:MoveTo(point.Position)
humanoid.MoveToFinished:Wait()
print("finished moving")
What solutions have you tried so far? I tried looking for solutions but found none. I don’t exactly know what the issue here is so that’s why Im asking for help on the devforum.
I just tried this and I see not issue at all using what you tried.
EDIT: using
wait(1)
local zombie = script.Parent
local humanoid = zombie.Humanoid
local point = workspace.Point
humanoid:MoveTo(point.Position)
print("eeeee")
humanoid.MoveToFinished:Wait()
print("finished moving")
The same issue happens with me, I would make a value and then a function which changes the value every time the zombie reaches a part.
local zombie = zombie
local Part = Part
local Value = Value -- somewhere
local function ZombieMoveToFinished()
Value.Value = Value.Value + 1
end)
zombie.Humanoid.MoveToFinished:Connect(ZombieMoveToFinished)
Value.Changed:Connect(function(property)
if property == Value.Value then
zombie.Humanoid:MoveTo(
game.Workspace:WaitForChild(Part.Name..Value.Value) -- Part1, Part2
)
end
end)
hopefully this helps, not the actual script change the variable and names of Parts/Points
So This si caused by the NPC going too slow ie the part are too far away,That movetofinished just skip and go do the next function,my fix is a repeat until loop that use a return function
local function Check(v)
if (script.Parent.PrimaryPart.Position-v.Position).Magnitude > 4 then
script.Parent.Humanoid:MoveTo(v.Position)
script.Parent.Humanoid.MoveToFinished:Wait()
print("Far From Position")
return false
elseif (script.Parent.PrimaryPart.Position-v.Position).Magnitude <= 4 then
print("At the position")
return true
end
end
The 4 is a prevention of the HRP being above the part and still too far,You can reduce that to suit you
local function moveTo(humanoid: Humanoid, targetPoint: Vector3, 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