Making function loop

Hi,
im trying to make a script which allows people to equip and unequip items in their GUI inventory.

I have made it so they can equip and unequip ( This works!) but when they go to equip again it doesnt go back to its original function.

		equip.MouseButton1Click:Connect(function(equipped)
			
			 clicks += 1
			
			equip.Text = "Equipped"
			
			local character = player.Character
			
			task.wait()
			
			local Head = character:FindFirstChild('Head') -- will wait until the head is loaded, at that time the code below will be yielded by this function, using waitforchild on something that doesnt exist will yield infinitely
			local Torso = character:FindFirstChild('Torso')
			local LowerTorso = character:FindFirstChild('LowerTorso') -- both has waistbackattachment
			local plrTrail = Trail:Clone()
			if Torso then
				plrTrail.Parent = Torso
				plrTrail.Attachment1 = Torso.WaistBackAttachment
			elseif LowerTorso then
				plrTrail.Parent = LowerTorso
				plrTrail.Attachment1 = LowerTorso.WaistBackAttachment
			end
				plrTrail.Attachment0 = Head.FaceFrontAttachment
				
				
				
			
			
			if clicks == 2 then
				
				clicks = 0
				
				equip.Text = "Equip"
				
				plrTrail:Destroy()
				plrTrail.Attachment0:Destroy()
				plrTrail.Attachment1:Destroy()
				
				
			end
			
				
				
				
			end)
				
	
	end
end

Heres the code im using for the equip and unequipping of the items. Preferably id like it s that when they go to equip for the second time it replays the proginal function ‘equipped’.

Thankyou

So when you equip/unequip it works but the second time you try it then it doesnt?

yes so first time it equips, works fine, then unequips, works fine again but when go to equip again it doesnt work Throws up the error “Wasitbackattachment is not a valid member of meshpart”

It looks like you delete those attachments on the second “click”. Im not 100% sure if you can do this, but try removing the trail instead? Since it seems like you create it at the start.

plrTrail:Destroy() doesnt work when its by itself

Try clearing the trail when disabled- plrTrail:Clear() got it here: https://developer.roblox.com/en-us/api-reference/property/Trail/Enabled the link might help you since there’s a section about clearing/disabling trails.

i tried using clear and it still doesnt work

Add prints, see where it stops, then debug.
Then you can let us know more.

done that and it cannot replay function(equipped)

Can you show us the entire script?

Shall you write for i , v in pairs(your things) do end?