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

how do i get a random member of a table?

Asked by 3 years ago

if I have a table with for instance

local g = {'hello','idk','more stuff'}
--please correct me if im using the wrong brackets--

how would i get something random from it?

1 answer

Log in to vote
1
Answered by
lilzy7 68
3 years ago
Edited 3 years ago

you would do something like this:

local g = {'hello','idk','more stuff'}

local variable = math.random(1,#g)
-- get random number from 1 to the table's length, which is 3 because there is 3 things inside

local randommember = g[variable]
-- get random member from table as you have a random number from 1 to the max stuff in the table, then use that number to pull a string(text), randomly.

print(randommember)
-- you get a random member from the table!
Ad

Answer this question