Equip multiple tools

Hello everyone in my Inventory System you can only equip one tool likie when you equip andything and press on another item u have to unequip the first one first but i want to be able to euip everything and it should check if that tool is equipped or no even if it’s multiple tools with the same name like what I want right now is to be able to equip every tool:


local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local inventoryStore = DataStoreService:GetDataStore("InventoryStore")
local inventoryEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("InventoryEvent")

Players.PlayerAdded:Connect(function(player)
	-- ✅ Create Inventory folder
	local inventory = Instance.new("Folder")
	inventory.Name = "Inventory"
	inventory.Parent = player

	-- ✅ Load saved inventory
	local success, data = pcall(function()
		return inventoryStore:GetAsync("Inventory_" .. player.UserId)
	end)

	if success and data then
		for _, itemName in ipairs(data) do
			local savedTool = ServerStorage:FindFirstChild("Saved") and ServerStorage.Saved:FindFirstChild(itemName)
			if savedTool then
				savedTool:Clone().Parent = inventory
				inventoryEvent:FireClient(player, itemName, true)
			else
				warn("Missing saved tool: " .. itemName)
			end
		end
	else
		if not success then
			warn("Failed to load inventory for " .. player.Name .. ": " .. tostring(data))
		end
	end

	-- Fire newly added items to UI
	inventory.ChildAdded:Connect(function(item)
		inventoryEvent:FireClient(player, item.Name, true)
	end)

	player:WaitForChild("PlayerGui"):WaitForChild("InventoryGui") -- Optional wait for GUI
end)

Players.PlayerRemoving:Connect(function(player)
	-- Save player's inventory to DataStore
	local inventory = player:FindFirstChild("Inventory")
	if not inventory then return end

	local itemsToSave = {}
	for _, item in pairs(inventory:GetChildren()) do
		table.insert(itemsToSave, item.Name)
	end

	local success, err = pcall(function()
		inventoryStore:SetAsync("Inventory_" .. player.UserId, itemsToSave)
	end)

	if not success then
		warn("Failed to save inventory for " .. player.Name .. ": " .. tostring(err))
	end
end)

-- Equip/Unequip handling
inventoryEvent.OnServerEvent:Connect(function(player, itemName, value, button)
	local inventory = player:FindFirstChild("Inventory")
	local selectedItem = inventory and inventory:FindFirstChild(itemName)
	if not selectedItem then return end

	local backpack = player:FindFirstChild("Backpack")
	local character = player.Character

	if value == false then
		local alreadyEquipped = character and character:FindFirstChildWhichIsA("Tool")

		if not alreadyEquipped and backpack and #backpack:GetChildren() == 0 then
			-- Equip
			selectedItem:Clone().Parent = backpack
			if button then
				button.Text = "Unequip"
				button.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
			end
		else
			-- Unequip
			if button then
				button.Text = "Equip"
				button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
			end
			for _, item in ipairs(backpack:GetChildren()) do
				if item:IsA("Tool") then item:Destroy() end
			end
			for _, item in ipairs(character:GetChildren()) do
				if item:IsA("Tool") then item:Destroy() end
			end
		end
	end
end)







second cod:




-- Ensure the InventoryEvent is correctly referenced
local InventoryEvent = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("InventoryEvent")

-- Ensure the itemFrame (which should be a ScrollingFrame) is correctly referenced
local itemFrame = script.Parent:WaitForChild("ItemsFrame")

-- Reference UI Elements
local searchTextBox = script.Parent:WaitForChild("SearchBox")
local equipFrame = script.Parent.Parent:WaitForChild("EquipFrame")
local equipButton = equipFrame:WaitForChild("EquipButton")
local equipTitle = equipFrame:WaitForChild("Title")
local toolEquippedLabel = script.Parent.Parent:WaitForChild("ToolEquippedLabel")

-- Color map for item rarity
local itemColorMap = {
	Common = Color3.fromRGB(95, 95, 95),  
	Epic = Color3.fromRGB(163, 39, 167),    
	Legendary = Color3.fromRGB(218, 212, 45), 
	Mythic = Color3.fromRGB(255, 31, 0),    
	Rare = Color3.fromRGB(240, 74, 146),    
	Uncommon = Color3.fromRGB(47, 255, 0) 
}

-- Tool to rarity mapping
local toolRarityMap = {
	["Basic Ball"] = "Common",
	["Legendary Ball"] = "Legendary",
	["Divine Ball"] = "Mythic",
	["Master Ball"] = "Epic",
	["Basic Mexo"] = "Common",
	["Rare Mexo"] = "Rare",
	["Uncommon Mexo"] = "Uncommon",
	["Epic Mexo"] = "Epic",
	["Legendary Mexo"] = "Legendary",
	["Godly Mexo"] = "Mythic"
}

-- Tool to image map (replace asset IDs with real ones)
local toolImageMap = {
	["Basic Ball"] = "rbxassetid://132012727834225",
	["Legendary Ball"] = "rbxassetid://93920317707502",
	["Divine Ball"] = "rbxassetid://100849630832562",
	["Master Ball"] = "rbxassetid://110597563645232",
	["Basic Mexo"] = "rbxassetid://119425421995534",
	["Rare Mexo"] = "rbxassetid://108005525588737",
	["Uncommon Mexo"] = "rbxassetid://96555382038798",
	["Epic Mexo"] = "rbxassetid://82264094050315",
	["Legendary Mexo"] = "rbxassetid://135664282641333",
	["Godly Mexo"] = "rbxassetid://119425421995534"
}

-- Function to filter the items based on the text in the TextBox
local function filterItems(searchText)
	for _, itemButton in pairs(itemFrame:GetChildren()) do
		if itemButton:IsA("TextButton") and itemButton.Name ~= "ItemButton" then
			if string.lower(itemButton.ItemName.Value):find(string.lower(searchText)) then
				itemButton.Visible = true
			else
				itemButton.Visible = false
			end
		end
	end
end

-- Listen for changes in the search TextBox
searchTextBox:GetPropertyChangedSignal("Text"):Connect(function()
	filterItems(searchTextBox.Text)
end)

-- Function to apply rainbow gradient
local function applyRainbowGradient(button)
	button.BackgroundTransparency = 1 -- make solid background transparent so gradient is visible

	local gradient = Instance.new("UIGradient")
	gradient.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0.0, Color3.fromRGB(255, 0, 0)),    -- Red
		ColorSequenceKeypoint.new(0.17, Color3.fromRGB(255, 127, 0)), -- Orange
		ColorSequenceKeypoint.new(0.33, Color3.fromRGB(255, 255, 0)), -- Yellow
		ColorSequenceKeypoint.new(0.5, Color3.fromRGB(0, 255, 0)),    -- Green
		ColorSequenceKeypoint.new(0.67, Color3.fromRGB(0, 0, 255)),   -- Blue
		ColorSequenceKeypoint.new(0.83, Color3.fromRGB(75, 0, 130)),  -- Indigo
		ColorSequenceKeypoint.new(1.0, Color3.fromRGB(148, 0, 211))   -- Violet
	})
	gradient.Rotation = 0
	gradient.Parent = button
end

-- Listen for events from the server to add/remove items from the inventory
InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value == true then
		local templateButton = itemFrame:FindFirstChild("ItemButton")
		if not templateButton then
			warn("ItemButton template not found inside ItemsFrame!")
			return
		end

		local itemCount = 1
		for _, existingButton in pairs(itemFrame:GetChildren()) do
			if existingButton:IsA("TextButton") and existingButton:FindFirstChild("ItemName") then
				if existingButton.ItemName.Value == ItemName then
					itemCount += 1
				end
			end
		end

		local uniqueItemName = ItemName .. "_" .. itemCount

		local ItemButton = templateButton:Clone()
		ItemButton.Visible = true
		ItemButton.Name = uniqueItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = itemFrame

		local itemNameValue = Instance.new("StringValue")
		itemNameValue.Name = "ItemName"
		itemNameValue.Value = ItemName
		itemNameValue.Parent = ItemButton

		local rarityCategory = toolRarityMap[ItemName]
		local color = itemColorMap[rarityCategory]

		if rarityCategory == "Mythic" then
			applyRainbowGradient(ItemButton)
		else
			if color then
				ItemButton.BackgroundColor3 = color
			end
		end

		local uiStroke = ItemButton:FindFirstChildOfClass("UIStroke") or Instance.new("UIStroke")
		uiStroke.Parent = ItemButton
		uiStroke.Thickness = 2
		uiStroke.Color = Color3.fromRGB(38, 38, 38)
		uiStroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
		ItemButton.BackgroundTransparency = 0.3

		local imageLabel = ItemButton:FindFirstChild("ImageLabel")
		if imageLabel then
			local imageID = toolImageMap[ItemName]
			if imageID then
				imageLabel.Image = imageID
			else
				warn("No image assigned for tool: " .. ItemName)
			end
		else
			warn("ImageLabel missing from ItemButton!")
		end

		ItemButton.MouseButton1Click:Connect(function()
			equipTitle.Text = ItemName
			equipTitle.Visible = true
			equipButton.Visible = true
			toolEquippedLabel.Text = "Selected: " .. ItemName
			toolEquippedLabel.Visible = false
		end)
	else
		for _, itemButton in pairs(itemFrame:GetChildren()) do
			if itemButton:IsA("TextButton") and itemButton:FindFirstChild("ItemName") then
				if itemButton.ItemName.Value == ItemName then
					itemButton:Destroy()
					break
				end
			end
		end
	end
end)