Backpack system breaks if there 2 of the same tools in inventory

I have a backpack system, and everything for it works fine, except this one bug I found.
If there are tools with the same name in your inventory, only the first tool will show up in the GUI, and is in general really buggy when trying to use the tool.
I really don’t know how to fix it so, I’ve been stuck on this for 30 minutes now.

The main backpack code:

	local main=script.Parent.Backpack
	game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	local function removeUI(ui)
		for _, v in pairs(ui:GetDescendants()) do
			vb.Tween(ui, {.15}, {Transparency=1}, true, false)
		end
		vb.Tween(ui, {.15}, {Size=UDim2.new(0,0,0,0), Transparency=1}, true, true)
		ui:Destroy()
	end
	local KeyPressed=Instance.new("BindableEvent")
	local function createUI(tool)
		local clone=script.Parent.Backpack.template:Clone()
		clone.toolname.Text=tool.Name
		clone.Name=tool.Name
		local desc
		if tool.ToolTip then
			desc=tool.ToolTip
		end
		
		clone.Visible=true
		clone.Parent=main
		clone.indicator.Text=tostring(#main:GetChildren()-2)
		clone:SetAttribute("equipped", false)

		
		--the rest of the setting up
		local function equip()
			if clone:GetAttribute("equipped")==false then
				clone:SetAttribute("equipped", true)
				vb.tweentab({
					{{clone}, .2, {BackgroundColor3=Color3.fromRGB(0, 50, 61)}},
					{{clone.UIStroke}, .2, {Color=Color3.fromRGB(0, 98, 118)}}})
				for _, v in pairs(script.Parent.Backpack:GetChildren()) do
					if v:IsA("Frame") and v~=clone then
						vb.tweentab({
							{{v}, .1, {BackgroundColor3=Color3.fromRGB(61, 61, 61)}},
							{{v.UIStroke}, .1, {Color=Color3.fromRGB(61, 61, 61)}},
							{{clone.indicator}, 1, {TextTransparency=1, TextStrokeTransparency=1}},
						})
						v:SetAttribute("equipped", false)
					end
				end
				lplr.Character.Humanoid:EquipTool(tool)
			else
				vb.tweentab({
					{{clone}, .25, {BackgroundColor3=Color3.fromRGB(61, 61, 61)}},
					{{clone.UIStroke}, .25, {Color=Color3.fromRGB(61, 61, 61)}},
					{{clone.indicator}, .25, {TextTransparency=1, TextStrokeTransparency=1}},})
				clone:SetAttribute("equipped", false)
				if desc then
					vb.Tween(script.Parent.BackpackToolDesc, {.1}, {BackgroundTransparency=.5, TextTransparency=0}, true, false)
					script.Parent.BackpackToolDesc.Text=desc
				end
				lplr.Character.Humanoid:UnequipTools()
			end
		end
		clone.ImageButton.MouseButton1Click:Connect(function()
			equip()
		end)
	end


	lplr.Backpack.ChildAdded:Connect(function(h)
		for _, v in pairs(main:GetChildren()) do
			if v.Name:sub(2)==h.Name then
				return
			end
		end
		if not main:FindFirstChild(h.Name) then
			createUI(h)
		end
	end)

	lplr.Backpack.ChildRemoved:Connect(function(h)
		wait()
		if not lplr.Character:FindFirstChild(h.Name) then
			removeUI(main:FindFirstChild(h.Name))
		end
	end)

	local keys={"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
	game:GetService("UserInputService").InputBegan:Connect(function(e)
		if table.find(keys, tostring(e.KeyCode):sub(14)) then
			KeyPressed:Fire(tostring(e.KeyCode):sub(14))
		end
	end)

vb is a predefined module that I made that handles tweening of all kinds.
Any help greatly appreciated!

1 Like