local teams = {red(), green(), blue(), yellow()} -- This line should pick one random index from the array.
Is your question that you want to call the function 'red' when the random index is 1?
If so, then the answer would be:
-- If you don't need to return anything from the call, then just remove the returns local teams = { function() return red() end, function() return green() end, function() return blue() end, function() return yellow() end } -- OR local teams = {} teams[1] = red teams[2] = green teams[3] = blue teams[4] = yellow -- AND THEN teams[math.random(1, #teams)]() -- This will call the function stored in the table
If not, then you should rephrase to "access an index" instead of "call an index" and your answer would just be:
teams[math.random(1, #teams)]
When calling an index from an array you use "[number]". Knowing this, you can use "math.random(minimumNumber, maximumNumber)" to generate a pseudo-random number. In order to obtain the amount of objects in an array, you use the "#" symbol followed by the name of the array.
teams = {red(), green(), blue(), yellow()} randomTeam = teams[math.random(1, #teams)];
This will work, though, if you call upon it when there are 0 objects in the array, you will get "nil" instead.
If you have any questions or issues, contact me. ;)
You can use the NextInteger function of Random.
local teams = {red(), green(), blue(), yellow()} print("Team " .. teams[Random.new():NextInteger(1, #teams)] .. " chosen!")