Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make a Random sort of text Script?

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

✓ 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

0

Thanks, it really helped.

0

Hey, DataStore, can I buy something from you? (I need to stop with these puns.)

This discussion is preserved as it was. New answers, comments, and votes are closed.