hi i have a hotbar and everything works in the hotbar as expected but it gives an error and i dont know what to do
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local plr = game.Players.LocalPlayer
local char = plr.Character
local deb = false
local backpack = plr:WaitForChild("Backpack")
local tools = backpack:GetChildren()
local slotMax = 9
local hotbar = script.Parent
local function setEquip(tool)
char.Humanoid:UnequipTools()
char.Humanoid:EquipTool(tool)
for x, slot in pairs(hotbar:GetChildren()) do
if slot:IsA("Frame") then
if tool.Parent ~= char then
slot.BackgroundColor3 = Color3.fromRGB(38,176,222)
else
slot.BackgroundColor3 = Color3.fromRGB(13, 62, 79)
end
end
end
end
local numToWord =
{
[1] = "E", -- to equip knife
[2] = "Q", -- to equip guns
}
local function updateHotbar()
for i, child in pairs(hotbar:GetChildren()) do
if child.Name == "Slot" then child:Destroy() end
end
for i, tool in pairs(tools) do
if i > slotMax then return end
local slotClone = script.Slot:Clone()
slotClone.ToolName.Text = tool.Name
slotClone.Parent = hotbar
if tool.Parent == char then
slotClone.BackgroundColor3 = Color3.fromRGB(13, 62, 79)
end
slotClone.ToolName.MouseButton1Click:Connect(function()
setEquip(tool)
end)
end
end
local function checkKeyPress(input)
-- First, we loop through all hotbar slots
for i=1, #hotbar:GetChildren() do
if input.KeyCode == Enum.KeyCode[numToWord[i]] and tools[i] then --error here
print(i)
setEquip(tools[i])
return
end
end
end
game:GetService("UserInputService").InputBegan:Connect(function(input, processed)
if not processed then
checkKeyPress(input)
end
end)
for _, tool in pairs(tools) do
if not table.find(tools, tool) then
table.insert(tools, tool)
updateHotbar()
end
end
backpack.ChildAdded:Connect(function(child)
if not table.find(tools, child) then
table.insert(tools, child)
updateHotbar()
end
end)
backpack.ChildRemoved:Connect(function(child)
if child.Parent ~= char and child.Parent ~= plr.StarterGear then
table.remove(tools, tools[child])
updateHotbar()
end
end)
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") and not table.find(tools, child) then
table.insert(tools, child)
updateHotbar()
end
end)
char.ChildRemoved:Connect(function(child)
if child.Parent ~= backpack then
table.remove(tools, tools[child])
updateHotbar()
end
end)
it a hotbar im making for a fps game and its gonna be like arsenal but using e and q to equip between knife and gun
Error
Players.Creeperman16487.PlayerGui.HotbarGui.Hotbar.HotbarHandler:82: invalid argument #2 (string expected, got nil)
the error only happens if i click any other key other than q and e but it equips correctly