Attachments keep cloning

I’m working on a trail system, and every time something is equipped it keeps cloning the attachments, how can i make it where it only clones the attachment once.

local repstorage = game:GetService("ReplicatedStorage")
local trails = game:GetService("ReplicatedStorage").Trails
local equippedTrail = {}

repstorage.TrailRemote.OnServerEvent:Connect(function(player, selectedbutton, equipped)
	if equipped then
		local trail = trails[selectedbutton]
		
		if trail then
			if equippedTrail[player] then
				equippedTrail[player]:Destroy()
			end
			
			local trailClone = trail:Clone()
			local character = player.Character
			trailClone.Parent = character.Head
			local attatchmentClone0 = Instance.new("Attachment", character.Head)
			attatchmentClone0.Name = "TrailAttatchment0"
			local attatchmentClone1 = Instance.new("Attachment", character.HumanoidRootPart)
			attatchmentClone1.Name = "TrailAttatchment1"
			trailClone.Attachment0 = attatchmentClone0
			trailClone.Attachment1 = attatchmentClone1
			equippedTrail[player] = trailClone
		end
		
	else
		
		if equippedTrail[player] then
			equippedTrail[player]:Destroy()
			equippedTrail[player] = nil
			
		end
	end
end)

Try this.

local repstorage = game:GetService("ReplicatedStorage")
local trails = game:GetService("ReplicatedStorage").Trails
local equippedTrail = {}

repstorage.TrailRemote.OnServerEvent:Connect(function(player, selectedbutton, equipped)
	if equipped then
		local trail = trails[selectedbutton]

		if trail then
			local character = player.Character
			
			if equippedTrail[player] then
				equippedTrail[player]:Destroy()
			end
			
			if character.Head:FindFirstChild("TrailAttatchment0") then
				character.Head:FindFirstChild("TrailAttatchment0"):Destroy()
			end
			
			if character.HumanoidRootPart:FindFirstChild("TrailAttatchment1") then
				character.HumanoidRootPart:FindFirstChild("TrailAttatchment1"):Destroy()
			end

			local trailClone = trail:Clone()
			trailClone.Parent = character.Head
			
			local attatchmentClone0 = Instance.new("Attachment", character.Head)
			attatchmentClone0.Name = "TrailAttatchment0"
			local attatchmentClone1 = Instance.new("Attachment", character.HumanoidRootPart)
			attatchmentClone1.Name = "TrailAttatchment1"
			trailClone.Attachment0 = attatchmentClone0
			trailClone.Attachment1 = attatchmentClone1
			
			equippedTrail[player] = trailClone
		end

	else

		if equippedTrail[player] then
			equippedTrail[player]:Destroy()
			equippedTrail[player] = nil

		end
	end
end)

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