Lua – if statement with two conditions on the same variable?

You should convert your string to a number, if you know for sure that it should be a number, and if there is no reason for it to be a string.

Here’s how to do a comparison for a range:

myVariable = tonumber(myVariable)

if (100000 >= myVariable and myVariable >= 80000) then

Notice the and. Most programming languages don’t expand the form x < y < z to x < y AND y < z automatically, so you must use the logical and explicitly. This is because one side is evaluated before the other side, so in left-to-right order it ends up going from x < y < z to true < z which is an error, whereas in the explicit method, it goes from x < y AND y < z to true AND y < z to true AND true, to true.

Leave a Comment