Quick question,
local Boxes = {"Box1", "Box2", "Box3"} -- How can I get a random value from this table?
Quick question,
local Boxes = {"Box1", "Box2", "Box3"} -- How can I get a random value from this table?
Well simple, just takes a bit of thinking
local Boxes = {"Box1", "Box2", "Box3"}
print(Boxes[math.random(1,#Boxes)])
so prrtty much, we will do Boxes[any number] to represent any member from that table, but if we want to be random? we gotta use math.random() which generates a random number btween the two given ones.
Now, arrays start at 1, so the first number is obviously 1, and the second one can be 3 cuz we know that we got 3 members, but sometimes you may not know how many memebers there are
so we use #. #Boxes will return 3 since ou table got 3 things inside it get it? Andt hat's it.
I'm aware that denny solved this but just wanted to explain it.
Hey there! so we are going to start off with our table simple right?
local Table = {"Box","Box2","Box3"}
So now we have our table we need to randomise right? so what we need to do is use a function called "math.random" so we start off with this:
local Table = {"Box","Box2","Box3"}
Table[math.random(1,#Table)]
So abit of an explaination so in our parameter we have a number what is that number for you ask? So we have 3 values in our table we go from 1 to the last value in the table which is 3.
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
you can use math.random pls check the wiki.
Boxes[math.random(1, #Boxes)]
wait, can you do that for tables to?
Wait, why didn't I think of that omg im stupid, thanks