Inventory System - (Problems with equipping tools with the same name)

Every-time I try unequipping an item with the same name, the item doesn’t unequip instead it stays in the character whilst the item with the same name stays in the backpack, I’m guessing this has something to do with the tool being removed and maybe the table but I don’t know what I would do. I can only unequip the item if I go onto one with another name

Black Leaf - Roblox Studio - Gyazo

Sample code:

function updateHotbar()
	for i,v in pairs(backpack:GetChildren()) do
		if v:IsA("Tool") then
			if Slot >= 9 and not table.find(ToolTable,v) then
				local Template = script.Template:Clone()
				Template.Parent = script.Parent.Inventory
				Template.toolName.Text = v.Name
				local gui = Template
			elseif Slot < 9 and not table.find(ToolTable,v) then
				Slot += 1
				table.insert(ToolTable,v)
				local Template = script.Template:Clone()
				Template.Parent = script.Parent.Container
				Template.toolName.Text = v.Name
				Template.NumberIndex.Text = Slot
				Template.Name = Slot						
			end
			
		end
	end
end

updateHotbar()

local magic = false
local fill = false
UIS.InputBegan:Connect(function(input, event)
	if event then return end
	if input.KeyCode == Enum.KeyCode.Backquote then
		inventory.Visible = not inventory.Visible
		local MaxSlots = 9
		local SlotsNeeded = MaxSlots - Slot
		for i= Slot, SlotsNeeded do
			local faketem = script.Template:Clone()
			faketem.Name = "fake"
			faketem.Parent = script.Parent.Container
			faketem.NumberIndex.Text = i+1
			faketem.toolName.Text = ""
			if inventory.Visible == false then
				for i,v in pairs(Hotbar:GetChildren()) do
					if v.Name == "fake" then
						v:Destroy()
					end
					updateHotbar()
					
				end
			end
		end
		
	end
	if keys[input.KeyCode.Name] and magic == false then
		if magic == true then return end
		local slot = Hotbar:FindFirstChild((keys[input.KeyCode.Name]))
		local tool = backpack:FindFirstChild(slot.toolName.Text)
		local charfind = char:FindFirstChild(slot.toolName.Text)
		if slot and tool ~= nil and table.find(ToolTable,tool) then
			Selection(slot)
			char.Humanoid:EquipTool(tool)
		else
			char.Humanoid:UnequipTools(tool)
			Selection(nil)
		end
	end
end)

backpack.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		if child.Parent == backpack and child.Parent ~= char then
	updateHotbar()
	table.insert(ToolTable,child)
	print(Slot)
end
end
end)


backpack.ChildRemoved:Connect(function(child)
	if child.Parent ~= char and child.Parent ~= backpack then
		for i,v in pairs(Hotbar:GetChildren()) do
			if v.ClassName == "Frame" and v.toolName.Text == child.Name then
				v:Destroy()
			--	Slot -= 1
			end
			updateHotbar()
		end
	end
end)

char.ChildRemoved:Connect(function(child)
	if child.Parent ~= char and child.Parent ~= backpack then
		for i,v in pairs(Hotbar:GetChildren()) do
			if v.ClassName == "Frame" and v.toolName.Text == child.Name then
				v:Destroy()
				Slot -= 1
			end
			updateHotbar()
		end
	end
end)
2 Likes

Use instance references (direct references to each tool) instead of string references (references to each tool’s ‘Name’ property).

2 Likes

Can you show me how would this look like?