math.random requires a number value to work. This is probably really simple I just can't find how to do it
COMMUNITY QUESTION
How to return the string value of a table using math.random?
1 answer
Tables use indices to reference data in the array. Depending on the form of the array—a dictionary or default form—the index type changes. Since we're using a basic table with string elements, we can use a numerical index form.
math.random will give us a random value from the given parameters, this will passed through in as the index form, telling the table to return us the randomized element from that given index.
I assume you understand tables by asking this question so I won't explain how they work extensively. Your code looks like this:
local stringTable = {"Hello", "world"};
print(stringTable[math.random(1,#Table)]) -- Telling random to give us a number between 1 and the amount of elements in the array
This discussion is preserved as it was. New answers, comments, and votes are closed.
