Cannot disconnect Humanoid.MoveToFinished function

Hey!

So, I’ve been making this steal a brainrot inspired game (steal a planet) recently and I have come up with an issue. When the planet reaches a player’s base, the MoveToFinished function, that is only supposed to be called for when the planet reaches the end, gets called. Even though I clearly disconnect it inside the planet spawn script, it still somehow stays after I call another MoveToFinished function. I dont know if this was explained clearly enough but yeah.

Planet Spawner Script:

local reached_end = false
			
			connection = chosen_planet.Humanoid.MoveToFinished:Connect(function()
				chosen_planet:Destroy()

				print("done")

				reached_end = true
				
				if connection then
					connection:Disconnect()
					connection = nil
				end
			end)
			
			chosen_planet.Humanoid:MoveTo(end_pos.Position)
			
			task.spawn(function()
				while not reached_end do
					if chosen_planet.Humanoid.WalkToPoint ~= end_pos.Position then
						print("path changed")
						
						connection:Disconnect()
						connection = nil
						
						break
					end
					
					chosen_planet.Humanoid:MoveTo(end_pos.Position)
					wait(7)
				end
				
				if connection then
					connection:Disconnect()
					connection = nil
				end
			end)

Purchase Script:

local reached_end = false

		connection = script.Parent.Humanoid.MoveToFinished:Connect(function()
			reached_end = true
			
			print("also done")
			
			for i, v in pairs(player.Base.Value:WaitForChild("Podiums"):GetChildren()) do
				if v:IsA("BasePart") then
					if v.Used.Value then
					else
						v.Used.Value = true
						
						break
					end
				end
			end
			
			connection:Disconnect()
			connection = nil
		end)
		
		script.Parent.Humanoid:MoveTo(player.Base.Value:WaitForChild("CollectPlanet").Position)
		
		task.spawn(function()
			while not reached_end do
				if script.Parent.Humanoid.WalkToPoint ~= player.Base.Value:WaitForChild("CollectPlanet").Position then
					break
				end

				script.Parent.Humanoid:MoveTo(player.Base.Value:WaitForChild("CollectPlanet").Position)
				wait(7)
			end
			
			if connection then
				connection:Disconnect()
				connection = nil
			end
		end)

Thanks

1 Like

chat gpt, copilot,grok3,deepseek
sorry my english is scripting is bad for help you

Using AI isn’t the best solution because you need to know what the code exactly does, if you don’t then it will lead to problems.

3 Likes