How do I check if a string is a number?

Hello, currently making a GUI for a lockdown system in my game, but I can get over this one hurdle. I am attempting to check if text in my textbox is a number. If not it won’t allow a lockdown to me initiated. I’m just looking for a solution if anyone has one.

local reason = GUI.Reason.TextBox.Text
local duration = GUI.Duration.TextBox.Text

GUI.ConfirmButton.MouseButton1Click:Connect(function()
if reason and duration and tonumber(duration) ~= nil then
	    print("yes")
    else
	    print("no")
    end
end)

Things I’ve tried,
I’ve tried doing :IsA() that just breaks.
Plus the code from above.
I cant find many other solutions online. So I’ve gone to the devforum for help.

11 Likes

TextBox.Text will always be a string, but to see whether it can be converted to a number or be used in math operations:

local str = TextBox.Text
if tonumber(str) then print("is number") end
 -- tonumber(var) returns nil if it can't be converted to a number

-- or to search for a number throughout entire string
if str:match("%d+") then print("contains number") end

Otherwise if you’ve got a variable,

local var = 5
if typeof(var) == "number" then print("number") end
5 Likes

You can use typeof() to do it as well.
Example:

local Text = tonumber(TextBox.Text)
if typeof(Text) == "number" then
-- Do the script.
end
6 Likes

But I changed it to a number, therefore it shouldn’t output a string.

3 Likes

No, the Text property itself will always be a string no matter what you do, you can call tonumber() though to convert it to its number form if it can be.

You can try that too.

if typeof(text) == "number" end won’t normally work, but since you converted it using tonumber() it will.

4 Likes

Reason and duration will always be strings so you don’t need to check if they exist or not. To validate whether or not an input is a valid number call tonumber on it. If the input is a valid number, the numeric value will be returned and otherwise nil.

GUI.ConfirmButton.MouseButton1Click:Connect(function ()
    local duration = tonumber(GUI.Duration.TextBox.Text)
    if duration then
        -- valid number
    else
        -- invalid number
    end
end)

If you want to extract the number from a string, this is a little more tricky than @XxELECTROFUSIONxX has suggested. The %d pattern only matches digits, however there are other characters which you would need to account for such as the negation operator.

39 Likes

There are some ways to extract the number. For example, he can string.gsub anything that’s not a digit, a negation unary operator -, or . for decimal numbers even.

local duration = tonumber(string.gsub(GUI.Duration.TextBox.Text, "[^-.%d]", ""))

Although this way is still not good, because something like "-56-4" becomes valid which wrong.

The better way would just be to use string.match with the right pattern. If he’s going to be accepting decimal places as well he would need to make a second pattern for it though.

local duration = tonumber(string.match(text, "%-?%d+") or string.match(text, "%-?%d+%.?%d+"))

But still, this is too convoluted and simply checking if tonumber(text) is valid is enough.

6 Likes

This did work. But after I made some modifications. Seems referencing .Text instantly breaks it. But referencing .Text after it works fine. I’m not too sure.

Thanks for your help everyone.

3 Likes