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)