Pet equip script

Hey there. I have made a pet equipping system for my simulator game, but for an unknown reason every time you equip a pet, it duplicates more than one time. For instance, let’s say I equip my dog, it works fine, but then I equip my cat. Instead of only having one cat equipped, I get an additional cat. This can happen infinitely, so if I equip my dog again i’ll have three dogs equipped instead of the intended one. I have tried Reviewing and fixing the code myself multiple times but can’t seem to find an answer.

module.EquipPet = function(petfolder, event, event2, plr, petinfo, pets, FailedEvent)
	
	event.OnServerEvent:Connect(function(plr, nameofpet)
		local Inventory = plr:FindFirstChild("Pets")

		if #plr.Character:WaitForChild("EquippedPets"):GetChildren() == 1 then
			for _, pet in pairs (petfolder:GetChildren()) do
				pet:Destroy()
			end
				
		elseif (Inventory:FindFirstChild(nameofpet)) and #plr.Character:WaitForChild("EquippedPets"):GetChildren() == 0 then
			task.delay(.5, function()
				Inventory[nameofpet]:Clone().Parent = plr.Character.EquippedPets
			end)
		end
	end)

	event2.OnServerEvent:Connect(function()
		if #plr.Character:WaitForChild("EquippedPets"):GetChildren() == 1 then
			for _, pet in pairs(petfolder:GetChildren()) do
				pet:Destroy()
			end
		end
	end)
end

Thanks in advanced!

nevermind i see the problem, you have a OnServerEvent:Connect() every time the equip pet is called so it will create a new connection every time you run this function so it will receive 1 signal first click 2 signals 2nd click so on.

i dont suggest it still very bad practice but instead of Connect you can do :Once
this will remote the connection after being fired once