Duplicated Swords on hip when equipping other swords

I use UserInputService KeyCode to equip and unequip tool

So I had a problem with sword on hip. players unable to equip one them since the new sword that taken from inventory is also on player hip.

image These’s 2 models are the sword on hip.

So my question is

How do I make player be able to equip a new sword from inventory and the current sword model that are on hip be destroyed?

Also I think there’s a problem with sword on hip or the sword inventory applier script but I don’t know how do i fix them.

Here’s are the scripts that I think made it not working

Sword On Hip Script:


Handle = nil

function onUnequipped()

	if script.Parent.Parent == workspace or script.Parent.Parent.className ~= "Backpack" then return end


	local char = script.Parent.Parent.Parent.Character
	if char ~= nil then
		local torso = char:FindFirstChild("Torso")
		local tool = char:FindFirstChild(script.Parent.Name)
		if torso ~= nil and tool == nil then
			local model = Instance.new("Model")
			model.Name = script.Parent.Name
			model.Parent = char

			handle = script.Parent.Handle:clone()
			handle.CanCollide = false
			handle.Name = script.Parent.Name
			handle.Parent = model

			local weld = Instance.new("Weld")
			weld.Name = "BackWeld"
			weld.Part0 = torso
			weld.Part1 = handle
			weld.C0 = CFrame.new(-0.9, -1.3,0.6)
			weld.C0 = weld.C0 * CFrame.fromEulerAnglesYXZ(math.rad(150), math.rad(-180), -20.5)
			weld.Parent = handle
		end
	end
end

script.Parent.Unequipped:Connect(onUnequipped)

function onEquipped()
	if handle ~= nil then 
		task.wait(0.3)
		handle.Parent:Destroy()
	end
end

script.Parent.Equipped:Connect(onEquipped)

UIS Keycode equip and unequip sword script:

local Equip = false
local debounce = false

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if Equip then
		if input.KeyCode == Enum.KeyCode.Q then
			if debounce then return end
			
			UnequippedAnimation:Play()

			debounce = true
			Equip = false
			UnequippedAnimation:GetMarkerReachedSignal("PutSword"):Connect(function()
				humanoid:UnequipTools(script.Parent)
			end)
			task.wait(1.2)
			debounce = false
		end
	elseif not Equip then
		if input.KeyCode == Enum.KeyCode.Q then
			if debounce then return end

			debounce = true
			Equip = true
			humanoid:EquipTool(script.Parent)
			task.wait(1.2)
			debounce = false
		end
	end
end)

For the sword inventory applier script, I think lines that are problem is here:

		for _, child in pairs(char:GetChildren()) do
			
			if child:IsA("Tool") then 
				
				if child.Name ~= swords.Name then
					child:Destroy() 
					
				else
					child:Destroy()
					
					plr.CurrentSwords.Value = ""
					
					return
				end
			end
		end

No errors in output

Any helps would be appreciated.

If you still don’t know what I mean by the vid here’s example:

As you can see the sword is unequipped and then when I equips a new sword, the sword on hip gone, and I be able to unequip and equip the new sword one

Nevermind I just figured it out.

			for _, child in pairs(character:GetChildren()) do
				
				if child:IsA("Model") then
					print("Model Detected")
					child:Destroy()
				end
			end

Put this in client sword gui shop script