Hi, I’m making a game where an NPC needs to reach an objective to trigger some code. However, MoveToFinished is never being fired, and other NPC’s need to push them into the right spot. I think this is a result of lag teleporting the NPC’s back, but I’m not sure. It is also giving an error "Humanoid is not a valid member of Model ", but I don’t think that has to do with this problem. Any help would be appreciated!
spawn(function()
local moveToFinished = false
clone.Humanoid.MoveToFinished:Connect(function()
game.ReplicatedStorage.BaseHealth.Value = game.ReplicatedStorage.BaseHealth.Value - (clone.Humanoid.Health / 20)
clone:Destroy()
moveToFinished = true
end)
for i = 1,100 do
clone.Humanoid:MoveTo(Vector3.new(51, 1, -29))
if moveToFinished == true then
break
end
wait(5)
end
end)
If MoveToFinished is not firing, try checking the distance between the clone and the target position you are trying to walk too:
spawn(function()
local targetPos = Vector3.new(51, 1, -29)
local root = clone.Humanoid.RootPart
local noYVector = Vector3.new(1,0,1)
while true do
clone.Humanoid:MoveTo(targetPos)
local dif = (root.Position - targetPos)*noYVector
if dif.magnitude < 2 then
game.ReplicatedStorage.BaseHealth.Value = game.ReplicatedStorage.BaseHealth.Value - (clone.Humanoid.Health / 20)
clone:Destroy()
break
end
wait(1)
end
end)
I’m getting the distance between the root part and the targetPos to make sure it’s less than 2, but I’m ignoring the Y axis.
Also, I didn’t test the code so there might be some syntax issues, but this should help you out.
This is an iteration loop which means that the code inside of it will run 100 times. Since wait(5) is inside of the for loop, if the event is never fired it will wait 500 seconds since 5*100 = 500.
I think the reason why Humanoid:MoveTo() is not firing is because you have not set up a connection. Here is an example on how to do that:
local function moveTo(humanoid, targetPoint)
local targetReached = false
-- Listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
end)
-- Start Walking
humanoid:MoveTo(targetPoint)
end