Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you check if text is a number?

Asked by 1 year ago

Basically, what I want to do is have the player able to put a amount of cash into a textbox and if the input isn't valid, the placeholder text will switch to "Not valid amount of cash!". If you know the solution to my problem please post below. Thanks

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

You can use the tonumber() function to convert a string to a number. If the string has no number representation then the function returns nil. This function will also return nil if the string has something other than a number; for example, tonumber("1234f") will return nil because it has a letter in it.

local TextBox = script.Parent

TextBox.FocusLost:Connect(function()

    local Amount = TextBox.Text

    if tonumber(Amount) then
        -- Code
    else
        TextBox.Text = ""
        TextBox.PlaceholderText = "Not a valid amount of cash!"
    end
end)

Documentation for Converting Strings

0
Thank you so much! masilasi2008 10 — 1y
Ad

Answer this question