Custom Inventory System Returning an Error

This can probably be solved with an :IsA()

Inventory.rbxm (13.5 KB)

I’ll quickly do that thank you above I attached the rbxm file.

Also Scroll down to the bottom the script *sorry about that!

So uh, funny thing. It works for me…

i quit how is it working for you??

I don’t honestly know?? It just adds the button…
image

alright but key presses dont work

when u hit 1 on your keyboard it works?

Yeah thats what im trying to make work.

local function mapNumberKeys()
	for index, ToolButton in ipairs(Frame:GetChildren()) do
		local keyNumber = index
		if keyNumber and keyNumber >= 1 and keyNumber <= 9 then
			toolMapping[keyNumber] = ToolButton
			print("Mapping key " .. keyNumber .. " to tool button " .. ToolButton.Name)
		end
	end
end

Found the issue…
Missing :IsA()

bruh thats what i did

local function mapNumberKeys()
	for index, ToolButton in ipairs(Frame:GetChildren()) do
		if ToolButton:IsA("Frame") then
			local toolName = ToolButton.Name
			local tool = Backpack:FindFirstChild(toolName)
			if tool and tool:IsA("Tool") then
				local keyNumber = index
				if keyNumber and keyNumber >= 1 and keyNumber <= 9 then
					toolMapping[keyNumber] = ToolButton
					print("Mapping key " .. keyNumber .. " to tool button " .. ToolButton.Name)
				end
			end
		end
	end
end
  • Forgot to add the codeblock in the first reply mb

Yeah!

  13:49:00.127  Key pressed: Two | Tool Button: nil  -  Client - GetPlayersTools LS:469
  13:49:00.127  No tool button found for index: 2

Ok quick update: I got the thing to map it to key 1 but it won’t unequip

I FIXED IT!!!, this is the code that fixed it!

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local Templates = script.Parent:WaitForChild("Template")
local ToolTemplate = Templates:WaitForChild("Tool")
local Frame = script.Parent:WaitForChild("Frame")

local equippedTool = nil

local toolMapping = {}

local function createToolButton(Tool)
    if Tool:IsA("Tool") then
        if Frame:FindFirstChild(Tool.Name) then
            return
        end

        local ToolButton = ToolTemplate:Clone()
        ToolButton.Name = Tool.Name
        local TextLabel = ToolButton:FindFirstChild("TextLabel")
        local ToolTipTextLabel = ToolButton.Tooltip:FindFirstChild("TextLabel")
        if TextLabel and ToolTipTextLabel then
            TextLabel.Name = Tool.Name
            TextLabel.Text = Tool.Name

            ToolTipTextLabel.Text = Tool.ToolTip
        else
            warn("TextLabel or ToolTipTextLabel not found in ToolTemplate")
        end

        local ClickButton = Instance.new("TextButton")
        ClickButton.Name = "ClickButton"
        ClickButton.Text = ""
        ClickButton.BackgroundTransparency = 1
        ClickButton.Size = UDim2.new(1, 0, 1, 0)
        ClickButton.Parent = ToolButton

        ClickButton.MouseEnter:Connect(function()
            script.Hover:Play()
            ToolTipTextLabel.Parent.Visible = true
        end)

        ClickButton.MouseLeave:Connect(function()
            ToolTipTextLabel.Parent.Visible = false
        end)

        ClickButton.MouseButton1Click:Connect(function()
            local Character = Player.Character or Player.CharacterAdded:Wait()
            local Humanoid = Character:FindFirstChildOfClass("Humanoid")
            local ActiveStroke = ToolButton:FindFirstChild("Active")

            if Humanoid then
                if equippedTool == Tool then
                    Humanoid:UnequipTools()
                    ActiveStroke.Enabled = false
                    equippedTool = nil
                else
                    Humanoid:UnequipTools()
                    if equippedTool then
                        local previousButton = Frame:FindFirstChild(equippedTool.Name)
                        if previousButton then
                            local previousStroke = previousButton:FindFirstChild("Active")
                            if previousStroke then
                                previousStroke.Enabled = false
                            end
                        end
                    end
                    Humanoid:EquipTool(Tool)
                    ActiveStroke.Enabled = true
                    equippedTool = Tool
                end
            end
        end)

        ToolButton.Parent = Frame
        ToolButton.Visible = true
    end
end

for _, Tool in ipairs(Backpack:GetChildren()) do
    createToolButton(Tool)
end

Backpack.ChildAdded:Connect(createToolButton)

local function mapNumberKeys()
    toolMapping = {} -- Clear existing mappings
    for _, ToolButton in ipairs(Frame:GetChildren()) do
        if ToolButton:IsA("Frame") then
            local toolName = ToolButton.Name
            local tool = Backpack:FindFirstChild(toolName)
            if tool and tool:IsA("Tool") then
                for keyNumber = 1, 9 do
                    if not toolMapping[keyNumber] then
                        toolMapping[keyNumber] = ToolButton
                        print("Mapping key " .. keyNumber .. " to tool button " .. ToolButton.Name)
                        break
                    end
                end
            end
        end
    end
end

mapNumberKeys()

Frame.ChildAdded:Connect(mapNumberKeys)

local function onKeyPress(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode
        if key.Value >= Enum.KeyCode.One.Value and key.Value <= Enum.KeyCode.Nine.Value then
            local index = key.Value - Enum.KeyCode.Zero.Value
            local toolButton = toolMapping[index]

            print("Key pressed: " .. key.Name .. " | Tool Button: " .. tostring(toolButton))

            if toolButton then
                local ToolName = toolButton.Name
                local Tool = Backpack:FindFirstChild(ToolName)
                print("ToolName: " .. ToolName .. " | Tool found: " .. tostring(Tool))

                if Tool then
                    local Character = Player.Character or Player.CharacterAdded:Wait()
                    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
                    local ActiveStroke = toolButton:FindFirstChild("Active")

                    if Humanoid then
                        print("Humanoid found. Equipped Tool: " .. tostring(equippedTool))

                        if equippedTool == Tool then
                            Humanoid:UnequipTools()
                            ActiveStroke.Enabled = false
                            equippedTool = nil
                            print("Tool unequipped: " .. ToolName)
                        else
                            Humanoid:UnequipTools()
                            if equippedTool then
                                local previousButton = Frame:FindFirstChild(equippedTool.Name)
                                if previousButton then
                                    local previousStroke = previousButton:FindFirstChild("Active")
                                    if previousStroke then
                                        previousStroke.Enabled = false
                                    end
                                end
                            end
                            Humanoid:EquipTool(Tool)
                            ActiveStroke.Enabled = true
                            equippedTool = Tool
                            print("Tool equipped: " .. ToolName)
                        end
                    else
                        print("No Humanoid found in character.")
                    end
                else
                    print("Tool not found in Backpack: " .. ToolName)
                end
            else
                print("No tool button found for index: " .. index)
            end
        end
    end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
    if not gpe then
        onKeyPress(input)
    end
end)

idk what i changed to make it work but something worked?

ok now it doesnt unequip yeah!

Ok I think I found it.
In the key pressed function it checks if the tool is in the backpack. But when you equip a tool it gets put in the Character Model.

ah ok, so it doesn’t technically exist to the script anymore If I’m correct?

Actually, something I observed while testing is If I switch to another tool it unequips the Previously equipped tool.