Checking if string is number?

Hello devs! Im working on custom commands and wanted to know if there’s a way to check if string is only number!

Hope you can help me!

Have a great day!

You can use tonumber using the string as an argument, if the string is not a full number, then it iwll return nil, if it is a full number, it returns the string converted to a number

local num = tonumber("10")
if num then
   print("Full number")
end

local num2 = tonumber("10jh")
if num2 then
    print("Full number")
end

Only the first FUll number will print because the other results in nil since it’s not a full strng number

I need to know if string is ANY number.

What do you mean by any number? tonumber will work for any number you give, even if you give it 101010502963868369087 as a string, it will still work for that so long as there’s no letters or punctuation marks

Any of the numbers, like 12345678 etc.

Yes, tonumber will still work for those numbers, it’ll work for any number you give it , even if it’s extremely high or a negative I believe. If there’s anything that isn’t a number in the string, it returns nil, so if you just check if the result is not nil, then that means a full number was given

No, I am making command like :settime 1 50 and i need to know if string is any of numbers you can write

Oh wait, i get it now… You need to do tonumber(args[1]) and check if its not nil!

1 Like

Thanks for help! You are always my life saver!

1 Like