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

How do you make a Random sort of text Script?

Asked by 9 years ago

I'm sorry if the Title question is a bit hard to understand, I'm a beginner and I don't know much.

So basically this is how the Script works:

print(math.random(1,5))
if 2 > 3 then
    print("ok")
elseif 2 < 3 then
    print("Lol")
end

This is how I want the script to work: If any number is above 2 then print("ok") but if any number is below 2 print("Lol")

I want one of you guys to break down the problem and explain please.

1 answer

Log in to vote
0
Answered by
DataStore 530 Moderation Voter
9 years ago
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.") 
0
Thanks, it really helped. BamBam201008 5 — 9y
0
Hey, DataStore, can I buy something from you? (I need to stop with these puns.) EzraNehemiah_TF2 3552 — 9y
Ad

Answer this question