Whenever player clicks on tool icon it equips the tools in a sequence not specifically what the player clicks

for starters i know my code is messy i’m just trying to make it work correctly and then brushing it up.

  1. Basically what i’m trying to do is make it to whenever the player clicks on the toolicon of the tool that shows up in their inventory gui that it will allow them to equip the tool that They have selected, but in this script it doesn’t do that.

  2. what this script is doing right now is actually letting me equip yes, But problem is it only equips them in a sequence so if i clicked the tool on slot 1 for example it will equip it but if i click on the second slot of another tool it won’t equip, it will equip the first slot before equipping the second slot.

  3. i tried getting help from friends and the ai chat thing but doesn’t help at all it’s annoying it actually kept messing up the code lol and i tried solutions i try finding out myself and i can’t seem to figure it out at all so i have no idea what to do anymore.

local open = script.Parent.Open
local frame = script.Parent.MainFrame
local scrollingframe = frame.ScrollingFrame
local ToolName = scrollingframe.ToolName
local Toolicon = scrollingframe.ToolName.toolicon


local function findItemIndex(items, itemName)
	for i, item in ipairs(items) do
		if item.name == itemName then
			return i
		end
	end
	return nil
end


local numSlots = 100000
local maxLimit = 999999

print("code is waiting for a new tool to be in backpack.. ")
local debounce = false -- debounce variable to prevent multiple clicks
local function createToolSlot(itemName, itemIcon)
	local newToolName = ToolName:Clone()
	local newToolIcon = Toolicon:Clone()
	newToolName.Name = "ToolName"
	newToolIcon.Name = "toolicon"
	newToolName.Text = itemName
	newToolIcon.Image = itemIcon
	newToolName.Parent = scrollingframe
	newToolIcon.Parent = newToolName
	newToolName.Position = UDim2.new(newToolName.Position.X.Scale, newToolName.Position.X.Offset, newToolName.Position.Y.Scale, newToolName.Position.Y.Offset + (50 * (#scrollingframe:GetChildren() - 1)))
	newToolIcon.Position = UDim2.new(newToolIcon.Position.X.Scale, newToolIcon.Position.X.Offset, newToolIcon.Position.Y.Scale, newToolIcon.Position.Y.Offset + (50 * (#scrollingframe:GetChildren() - 1)))
	newToolIcon.MouseButton1Click:Connect(function()
		local player2 = script.Parent.Parent.Parent
		local character = player2.Character
		local backpack = player2:FindFirstChildOfClass("Backpack")
		local tool = backpack:FindFirstChild(itemName)
		if tool then
			character.Humanoid:EquipTool(tool)
		end
	end)
end

local function updateInventory(player)
	local function equipTool(toolIcon)
		if debounce then -- check if the function is already running
			return
		end

		debounce = true -- set debounce variable to true to prevent multiple clicks

		local player2 = script.Parent.Parent.Parent
		local character = player2.Character
		local backpack = player2:FindFirstChildOfClass("Backpack")

		if not backpack then
			return
		end

		-- get the selected tool name from the ToolName label that corresponds to the ToolIcon button that was clicked
		-- get the selected tool name from the ToolName label that corresponds to the ToolIcon button that was clicked
		local itemName = ToolName.Text

		-- get the tool from the backpack using the selected tool name
		local tool = backpack:FindFirstChildOfClass("Tool")
if tool then equipTool()
		if not tool then
			print"tool not found"
			return
			end
			end

		-- unequip the tool if it's already equipped, or equip it if it's not
		local equippedTool = character:FindFirstChildOfClass("Tool")
		if equippedTool == tool then
			character.Humanoid:UnequipTools()
		else
			if equippedTool then
				equippedTool.Parent = backpack
				wait() -- wait for the tool to be put back into the backpack
			end

			-- equip the new tool
			character.Humanoid:EquipTool(tool)
		end

		debounce = false -- reset debounce variable
	end


	local items = {}
	local player2 = script.Parent.Parent.Parent
	local character = player2.Character
	local backpack = player2:FindFirstChildOfClass("Backpack")

	if backpack then
		for _, item in ipairs(backpack:GetChildren()) do
			if item:IsA("Tool") then
				local itemName = item.Name
				local itemIndex = findItemIndex(items, itemName)
				if not itemIndex and #items < numSlots and not scrollingframe:FindFirstChild("ToolName" .. #items+1) then
					print"trying to load rest"
					local toolIcon = item.TextureId or ""
					table.insert(items, {name = itemName, icon = toolIcon})
					item.Changed:Connect(function(property)
						if property == "Name" or property == "TextureId" then
							updateInventory(player)
						end
					end)
			

	if #items > 0 then
		for i = 1, #items do
			local player2 = script.Parent.Parent.Parent
			local character = player2.Character
			local backpack = player2:FindFirstChildOfClass("Backpack")
			local itemName = ToolName.Text
			local item = backpack:FindFirstChild(itemName)
			local newToolName = ToolName:Clone()
			local newToolIcon = Toolicon:Clone()
			newToolName.Name = "ToolName"
			newToolIcon.Name = "toolicon"
			newToolIcon.Image = items[i].icon
			newToolName.Parent = scrollingframe
			newToolIcon.Parent = newToolName
			newToolName.Position = UDim2.new(newToolName.Position.X.Scale, newToolName.Position.X.Offset, newToolName.Position.Y.Scale, newToolName.Position.Y.Offset + (50 * (i-1)))
			newToolIcon.Position = UDim2.new(newToolIcon.Position.X.Scale, newToolIcon.Position.X.Offset, newToolIcon.Position.Y.Scale, newToolIcon.Position.Y.Offset + (50 * (i-1)))
			newToolIcon.MouseButton1Click:Connect(function()
				equipTool(item)
			end)

		end
	else
		ToolName.Text = ""
		Toolicon.Image = ""
	end
end
			end
		end
	end
	end


open.MouseButton1Click:Connect(function()
	frame.Visible = not frame.Visible
	print"updatedinventory"
	updateInventory(game.Players.LocalPlayer)
end)


game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character
	character.ChildAdded:Connect(function(child)
		if child:IsA("Tool") then
			updateInventory(player)
		end
	end)

	character.ChildRemoved:Connect(function(child)
		if child:IsA("Tool") then
			updateInventory(player)
		end
	end)
end)

check this line out this is where it was going to work originally but it was not working at all it broke the script so i just changed it to the way it is right now so the script runs. and decided to just not care about it anymore lol

still need help with the code still have same issue still no fix

Still need help? i just need to know how to make it not equip tools in a sequence