Hotbar Works but Giving Error

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

It looks like the size of hotbar is larger than your numToWord table, which means that, sometimes, you’re checking if input.KeyCode == Enum.KeyCode[nil].

so i changed it now it seems that its still kinda of a way buggy where i have a round system where it spawns players then make the player equip guns and then put a knife in their backpack but i have a problem where soemtimes the knife slot wont show up

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 = 2


local hotbar = script.Parent

local function setEquip(tool)
	char.Humanoid:UnequipTools()
	wait()
	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(255, 255, 255)

			else
				slot.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
			end
		end
	end
end


local numToWord = 
	{
		[1] = Enum.KeyCode.E,  -- to equip knife
		[2] = Enum.KeyCode.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(0, 0, 0)
		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 == 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 game:GetService("UserInputService").KeyboardEnabled and not game:GetService("UserInputService").TouchEnabled and 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 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)

the latest code after i fixed the error