Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to pick one from many lists?

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 comment

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.

1 answer

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

2 comments

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.