✓ Accepted answer
local RandomNumber = math.random(1,5)
if RandomNumber > 2 then
print("The number is greater than two")
else
print("The number is equal to, or smaller than, two")
end
In the above example code we've created a variable. This variable is 'named' "RandomNumber", which we've set to math.random(1,5) (meaning that the variable will equal whatever is returned by the math.random() function).
Below this, we've used an if statement with the variable we created. On line 3 we're checking whether RandomNumber is 'greater than' two (3, 4, 5), if it is it will print what's on line 4. If RandomNumber isn't larger than two (1, 2) then it will print what's on line 6.
You could use an elseif statement (instead of an else statement), if you wanted, but there's really no need in this scenario. But if you did want to do this, you'd replace line five with the following:
elseif RandomNumber <= 2 then
For more information on conditional statements, see here.
However, you could do exactly this in 'two lines' using ternary operators, though if you do use this way it decreases the readability of your script (as you can probably tell).
local RandomNumber = math.random(1,5)
print(RandomNumber .. " is " .. (RandomNumber > 2 and "greater than" or "equal to, or smaller than") .. " 2.")
2 comments
Thanks, it really helped.
Hey, DataStore, can I buy something from you? (I need to stop with these puns.)