Custom Inventory System Returning an Error

Hello Everyone, I’m trying to make a custom Inventory system but my Number System for the keybinds is returning errors everytime I hit a key

Error:
Players.hjhfh263267nhj.PlayerGui.Main.Menu.Middle.Inventory.GetPlayersTools LS:187: attempt to compare EnumItem <= EnumItem - Client - GetPlayersTools LS:187

Function Returning the error:

local function onKeyPress(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode
        if key >= Enum.KeyCode.One and key <= Enum.KeyCode.Zero then
            local index = key - Enum.KeyCode.One
            local toolButton = toolMapping[index]
            if toolButton then
                local ToolName = toolButton.Name
                local Tool = Backpack:FindFirstChild(ToolName)
                if Tool then
                    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
            end
        end
    end
end

My Full code:

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()
	for index, ToolButton in ipairs(Frame:GetChildren()) do
		local keyNumber = tonumber(ToolButton.Name)
		if keyNumber and keyNumber >= 0 and keyNumber <= 9 then
			toolMapping[keyNumber] = ToolButton
		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 >= Enum.KeyCode.One and key <= Enum.KeyCode.Zero then
			local index = key - Enum.KeyCode.One
			local toolButton = toolMapping[index]
			if toolButton then
				local ToolName = toolButton.Name
				local Tool = Backpack:FindFirstChild(ToolName)
				if Tool then
					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
			end
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

I believe your issue is you are trying to compare a EnumItem like a number,
What you could try doing is comparing their Value instead

local FirstKeyCode,SecondKeyCode = Enum.KeyCode.Zero,Enum.KeyCode.Nine
print(FirstKeyCode.Value,SecondKeyCode.Value) --this will print 48 and 57.

I hope this helps!

oh this makes some sense give me a few minutes to add this and i’ll reach back when i test it.

Ok so now i’m getting this:
Players.hjhfh263267nhj.PlayerGui.Main.Menu.Middle.Inventory.GetPlayersTools LS:187: attempt to compare number <= EnumItem

code:

local function onKeyPress(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		if key >= Enum.KeyCode.One.Value and key <= Enum.KeyCode.Zero.Value then
			local index = key - Enum.KeyCode.One.Value
			local toolButton = toolMapping[index]
			if toolButton then
				local ToolName = toolButton.Name
				local Tool = Backpack:FindFirstChild(ToolName)
				if Tool then
					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
			end
		end
	end
end

I think you should have it be like this

local function onKeyPress(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key: Enum.KeyCode = input.KeyCode
		if key.Value >= Enum.KeyCode.Zero.Value and key <= Enum.KeyCode.Nine.Value then
			local index = key.Value - Enum.KeyCode.Zero.Value
			local toolButton = toolMapping[index]
			if toolButton then
				local ToolName = toolButton.Name
				local Tool = Backpack:FindFirstChild(ToolName)
				if Tool then
					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
			end
		end
	end
end

hope this helps :slight_smile:

I tried this but its still returning the compare error.

Could you send the error message?

image

Mind showing me line 228 of that script so I can focus on the problem point?

Line 228:

if key.Value >= Enum.KeyCode.Zero.Value and key <= Enum.KeyCode.Nine.Value then

Strange…
It says it can’t compare an Enum item to a number but both of them are number values?

WAIT IT SAYS key <= Enum.Keycode.Nine.Value
THE key IS MISSING .Value

Ok what could we do to fix this?

Ok so when i added the .Value there is no more error but my tools don’t equip

I added some debugging @Tijabedaboi
This is what my code returns:

  13:39:59.011  Key pressed: Two | Tool Button: nil  -  Client - GetPlayersTools LS:273
  13:39:59.012  No tool button found for index: 2  -  Client - GetPlayersTools LS:316
  13:39:59.462  Key pressed: Two | Tool Button: nil  -  Client - GetPlayersTools LS:273
  13:39:59.462  No tool button found for index: 2  -  Client - GetPlayersTools LS:316

Unfortunately, the errors stem from sources outside the script so solving this will be complicated.

I think i might have found the issue one second.

If that doesn’t work, could you potentially send me the .rblx file so I can look at it?

image

Ok so this is actually kinda funny

Yeah one second I can send it.