DescendantRemoving:Connect returns too many times

Hello, I would be grateful for some help!

I want to clone the part that falls into the void, back to a safe spot.
So I’m using a part as a parent for a script and an unanchored part,
the script handles a loop which looks for the unanchored part’s death and clones it

local RespawnPoint = script.Parent
local RespawnPoincf = RSP.CFrame

while true do	

	RespawnPoint.DescendantRemoving:Connect(function(part)		
		if part.Name == "PartOrignial"  then
			print("part destroyed")
			local clone = part:Clone()
			clone.Parent = script.Parent
			clone.CFrame = RespawnPoincf 
			
		end
	
	end)
	wait(1)
end

Issue : when it does clone, it does it more than once. (it makes many clone parts)
It would be awesome to know why DescendantRemoving:Connect fires like 4 sometimes 6 or 9 times.
And mainly how to make it so it only makes one cloned part. Thank you!

because you create a new connection every 1 second without disconnecting the previous one

you don’t need a while loop for this, just set up one connection and it’ll work until you disconnect it

Holy crud you are right, thank you very much! I did not notice that.

Just leaving this here just in case anyone needs it

local RespawnPoint = script.Parent
local RespawnPoincf = RSP.CFrame

RespawnPoint.DescendantRemoving:Connect(function(part)		
		if part.Name == "PartOrignial"  then
			print("part destroyed")
			local clone = part:Clone()
			clone.Parent = script.Parent
			clone.CFrame = RespawnPoincf 
			
		end
	
end)

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