MoveToFinished not firing

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)
2 Likes

if doesnt wait, so I’m not sure why you have this for loop set up the way you do

I have the loop set up so the timer does not run out.

what timer are you speaking of? That doesn’t clarify much

I mean the timer of 8 seconds that if the npc does not reach the goal in 8 seconds it will stop.

This is currently set up that it’s not going to stop for atleast 500 seconds.

How should I do this if a loop like this won’t work?

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.

for i = 1,100 do

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

I tried to do that, but it would not work. I think the only viable solution is to check the magnitude.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.