Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to check if string is an integer?

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

5 comments

0

You can find all numbers using %d+ pattern

0

Or you can replace all non numbers with gsub and the pattern %D+

0

The tonumber(...) function returns nil if conversion fails -> if tonumber(string) ~= nil then

0

~= nil's redundany as the if statement will check if a value's true or not.

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.