If GUI text is greater than ___, do ____

Hello! I am trying to make a server-wide money system that shows up via a GUI.

Is there a way to make it so if the number on said GUI is greater than a certain number, something happens? (Such as buying something)
If not, is there a way to do something similar without using a leader stat, because this is a server-wide GUI not something that differs depending on the user.

Note: I know this is a little off-topic, I didn’t know where to put it.

2 Likes

If it’s an int value, do if value >= 50 then
If it’s a string, do if tonumber(text) >= 50 then

Replacing the variables of course with the actual ones

You’ll need to convert the text into a number using the ‘tonumber()’ global and then you’ll need to use one of the various comparison/relational operators to compare it with something, i.e;

local TextBox = script.Parent --Text box that contains some text.
local Number = tonumber(TextBox.Text) --Convert the textbox's text into a number.
--Compare the number with something here.
1 Like
local TextLabel = script.Parent
local NumberConvert = tonumber(TextLabel.Text)
local requiredNumber = 100

if NumberConvert => requiredNumber then
    print(string.format("Player has enough cash available! (%s)", NumberConvert))
end

You don’t need to convert string to a number. When comparing a string to a number, lua will automatically try to convert the string into a number.

1 Like

That should be >=. Other than that looks fine (tho you should be using tonumber())

They achieve the same thing, I did use tonumber()?

Oh sorry, I didn’t realize you used the tonumber() on the variable, my apologies.

Didn’t know => is valid syntax (and I’m on mobile so I couldn’t check)

I don’t think => is a valid syntax for binary operation, though I might be wrong as I only had a quick look at the grammar of Luau (Grammar - Luau), and the binop doesn’t seem to include ‘=>’ but it does include ‘>=’.

binop = '+' | '-' | '*' | '/' | '^' | '%' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | 'and' | 'or'

cc @jmesrje

You are wrong. I use the same style In all of my scripts, they work expectedly.

Attempting "Hello world!" > 0" will result in an error. Therefore tonumber()'s use is definitely necessary.

The textbox’s text should be wrapped inside a call to ‘tonumber()’ which will return either a number (if the value could be converted) or nil (if the value couldn’t be converted).

1 Like

If you can make the assumption that the text is a number (because you’re the one setting it), then a tonumber call is completely unnecessary. If you cannot make that assumption, then the call can be necessary.

In the example snippet I provided I used a ‘TextBox’ instance, I was under the assumption that whatever value is being compared has been user provided, hence tonumber()'s use is essentially required (you could opt to use pcall()/xpcall() instead but both are of lesser performance).

image

Just so you’re aware, Lua only performs automatic coercion of values in certain contexts, that does not include operands in comparison operations.

2 Likes