As you can see I have two lists here, I want one out of the two to get picked by random, how can I do that?
local osv = {"Hi, can I have one scoop of vanilla?", "I would like one scoop of vanilla ice cream", "Can you get me a scoop of vanilla?"} local osc = {"Hi, can I have one scoop of chocolate?", "I would like one scoop of chocolateice cream", "Can you get me a scoop of chocolate?"}
You can use "Tables in tables." In tables, the value can be another table as well!
Let me show you!
local stuff = {{"Hi, can I have one scoop of vanilla?", "I would like one scoop of vanilla ice cream", "Can you get me a scoop of vanilla?"},{"Hi, can I have one scoop of chocolate?", "I would like one scoop of chocolateice cream", "Can you get me a scoop of chocolate?"}} -- List all of the tables in an array. local chosenTable = stuff[math.random(1, #stuff)] -- Picks a random table from the list. for i, v in ipairs(chosenTable) do print(i, v) -- DO STUFF! end
This will pick a random table from the list, then print all of its contents (including the indices).
You can also use variables for each table as well.
local osv = {"Hi, can I have one scoop of vanilla?", "I would like one scoop of vanilla ice cream", "Can you get me a scoop of vanilla?"} local osc = {"Hi, can I have one scoop of chocolate?", "I would like one scoop of chocolateice cream", "Can you get me a scoop of chocolate?"} local listOfExpressions = {osv, osc} local chosenList = listOfExpressions[math.random(1, #listOfExpressions)] -- Picks a random table from the list. for i, v in ipairs(chosenList) do print(i, v) -- DO STUFF! end
This also works the same!
UPDATE! If you wanted to have only one entry from the many lists, you just have to index it like so:
local osv = {"Hi, can I have one scoop of vanilla?", "I would like one scoop of vanilla ice cream", "Can you get me a scoop of vanilla?"} local osc = {"Hi, can I have one scoop of chocolate?", "I would like one scoop of chocolateice cream", "Can you get me a scoop of chocolate?"} local listOfExpressions = {osv, osc} local chosenList = listOfExpressions[math.random(1, #listOfExpressions)] -- Picks a random table from the list. local chosenExpression = chosenList[math.random(#chosenList)] -- Gets a random string value from the list chosen at random. print(chosenExpression) -- Prints the string in the output.
Any questions? Please leave a comment below. Thanks! :)