How do I tell if an input is a number input?

What I need


So I have a script,
And I need to be able to tell if the the input is a number. and not a letter or symbol.


This is using UserInputService


Thanks!

~Froyo~


What do you mean by “number”, As in Number Keys?

If you are referring to the Number Keys, they are named accordingly:
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine

Yes but I dont want to have an if statement for every individual number.
Thats why I am asking if there is away around this.

You can use typeof to get the datatype and figure this out. Here is an example:

local myVariable = 42

if typeof(myVariable) == "number" then
	print("myVariable is a number")
else
	print("myVariable is not a number")
end

Does this work for Enum.Keycode.One?

You could make a dictionary of the valid values and check it when you need to:

local isDigit = {
	[Enum.KeyCode.Zero] = true,
	[Enum.KeyCode.One] = true,
	[Enum.KeyCode.Two] = true,
	[Enum.KeyCode.Three] = true,
	[Enum.KeyCode.Four] = true,
	[Enum.KeyCode.Five] = true,
	[Enum.KeyCode.Six] = true,
	[Enum.KeyCode.Seven] = true,
	[Enum.KeyCode.Eight] = true,
	[Enum.KeyCode.Nine] = true,
}


	if isDigit[inputObject.KeyCode] then
		print("ISDIGIT")
	end

Just remember, backpack items are changed with the digit keys.

1 Like

Enum.Keycode.One is not a number so it would return as not a number.

Let’s try a different approach to determine if the keycode is a number or not.

-- KeyPressed function to detect if a key pressed is a number
local function KeyPressed(inputObject)
    local keyCode = inputObject.KeyCode
    if keyCode.Value >= Enum.KeyCode.Zero.Value and keyCode.Value <= Enum.KeyCode.Nine.Value then
        print("A number key was pressed!")
    end
end

-- Connect the KeyPressed function to the UserInputService's InputBegan event
game:GetService("UserInputService").InputBegan:Connect(KeyPressed)

1 Like

Thank you!
I will try this out!



As for this, yes I have done this. I am not going to have tools so thx