I have a TextBox and I need to check if the player has input integer (no letters or special symbols)
Something like:
if (string.isInteger) == true/false
COMMUNITY QUESTION
How to check if string is an integer?
3 answers
local isaint = tonumber(script.Parent.Text)
if isaint then
-- valid number
else
--invalid number
end
1 comment
0
this is a really good method, especially in vanilla lua but requires it to be converted first to a string which is a bit ucky when we could round the number and compare :)
function isInt(var)
if type(var) = "int" then
return true
else
return false
end
end
1 comment
0
This sadly won't work as one roblox doesn't acknowledge shorthand and second they don't have number types, only 'number' which could be a float or integer.
The simplest and most performant solution is to attempt to round the number then compare, if it's an integer it'll be equivelant.
local function isInteger(num)
if math.floor(num) == num then
return true
end
return false
end
This discussion is preserved as it was. New answers, comments, and votes are closed.

5 comments
https://stackoverflow.com/questions/10962085/lua-string-to-int
You can find all numbers using %d+ pattern
Or you can replace all non numbers with gsub and the pattern %D+
The
tonumber(...)function returns nil if conversion fails ->if tonumber(string) ~= nil then~= nil's redundany as the if statement will check if a value's true or not.