MoveToFinished Repeats

Every time the ais reach the goal, the movetofinish is fired. But each time it is, it is stacked.

For example, each time it finishes, it prints done. First time it finishes, it prints once. Second time, it prints twice, third, prints three times, etc. So the event is fired once, then twice, etc. How do I stop this?

function move(ai,ball)
	if ball ~= nil then
		wait()
		ai.Humanoid:MoveTo(ball.Position)
		ai.Humanoid.MoveToFinished:Connect(function()
			print("finished")
			local root = ai:FindFirstChild("HumanoidRootPart")
			if root ~= nil then
				root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0,math.rad(ball.Orientation.Y),0)
			end
		end)
	end
end

I’ve solved it, thanks though.

function move(ai,ball)
	local done = false
	if ball ~= nil then
		wait()
		ai.Humanoid:MoveTo(ball.Position)
		ai.Humanoid.MoveToFinished:Connect(function()
			if not done then
				print("finished")
				done = true
				local root = ai:FindFirstChild("HumanoidRootPart")
				if root ~= nil then
					root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0,math.rad(ball.Orientation.Y),0)
				end
			end
		end)
	end
end

I’m not sure why this is happening but a simple fix would probably be to just add a debounce to get the desired behavior.

local Finished

ai.Humanoid.MoveToFinished:Connect(function()
	if not Finished then
		Finished = true 
		
		local root = ai:FindFirstChild("HumanoidRootPart")
		if root ~= nil then
			root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0,math.rad(ball.Orientation.Y),0)
		end
	end
end)
1 Like

Yup, had the same idea. Sorry for the late reply, lol.