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

How to pick one from many lists?

Asked by
Seyfert 90
7 years ago

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?"}
1
Why not just make it one list? If it really matters, you can just pick a random table then pick a random value out of that. Perci1 4988 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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! :)

0
@CStarLam Hey this is really good, the only issue I am finding is that the for i, v in ipairs part is printing all of my entries in the list, I only want one of the entries to be picked. Is this possible? Would I have to make each entry a table and just have a table inside of a table etc. ? If I try to just do print(chosenList) I get something like "table 35652" in output Seyfert 90 — 7y
0
No You dont need to. You just can retreive this using chosenlist[here insert the number of string's position in table. for example to get "Hi, can I have one scoop of chocolate/vanilla?" place here 1 because its first in table] and it will return vanilla or chocolate (depends on chosen table) Etheroit 178 — 7y
Ad

Answer this question