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

How do I make a script that chooses something randomly out of a set?

Asked by
314cake 17
6 years ago

Like instead of picking randomly 1 through 5, the set would be (1,4,7,18,224) or something like that and it would pick randomly from that.

3 answers

Log in to vote
0
Answered by
xEiffel 280 Moderation Voter
6 years ago

To do this you'll have to use the math.random() method and create a table.

local table = {1, 4, 7, 18, 224} -- You can input any numbers or strings that you wish to add.

local RANDOM_NUMBER = table[math.random(#table)] -- Here is the variable getting an arbitrary number from the table above.

print(RANDOM_NUMBER) -- Printing the number that was randomly chosen from the table.

You can learn more about Tables and Mathematical Functions Here:

http://wiki.roblox.com/index.php?title=Table

http://wiki.roblox.com/index.php?title=Random_numbers

http://wiki.roblox.com/index.php?title=Global_namespace/Mathematical_functions

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I think you need a table, which works like this:

Table = {"1","2","3"}
Random = Table[math.random(#Table)]
Log in to vote
0
Answered by 6 years ago

Something along those lines

Table = {1,4,7,18,224}
NumberChosen = Table[math.random(#Table)]
print(NumberChosen)

Answer this question