Is there a way to set keyboard inputs 1-9 efficiently in hotbar system?

im making a hotbar system and im not trying to make an if statement for every single input to correspond with the hotbar.

image

it says “index” but when the game starts it will show the numbers from 1-9

i dont want to make 9 different if statements to do this, is there a way i can do this efficiently? or do i have to make 9 different if statements?

and input.KeyCode shows this

image

I ran into the same problem, I don’t think there’s any solution for that.

atleast its good to know that im not the only one with this issue

I found this article by someone trying to efficiently make a hot bar system similar to yours. Reading through it could be helpful!

edit: if this article isn’t much help I can look into it more and offer more assistance

2 Likes
local UserInputService = game:GetService("UserInputService")

local Enums = {[Enum.KeyCode.One] = 1, [Enum.KeyCode.Two] = 2, [Enum.KeyCode.Three] = 3, [Enum.KeyCode.Four] = 4, [Enum.KeyCode.Five] = 5, [Enum.KeyCode.Six] = 6, [Enum.KeyCode.Seven] = 7, [Enum.KeyCode.Eight] = 8, [Enum.KeyCode.Nine] = 9, [Enum.KeyCode.Zero] = 10}

UserInputService.InputBegan:Connect(function(Key, Processed)
	if Processed then
		return
	end

	local KeyCode = Enums[Key.KeyCode]
	if KeyCode then
		print(KeyCode.." pressed.")
	end
end)

Tables and loops are your friends.