# How would I randomly get more than one value from a table?

So basically I have a bunch of parts in the workspace all under a folder, I make a table by doing :GetChildren() of the folder, and I want to pick three random parts from the folder, how would I do that?

1 Like

I think you would have to rename all the parts with the same name and at the end put a number, then use math.random to pick a random one between them, repeat this tree times and you will get a what you nees, something like this:

``````for 1, 3 do
math.random(â€śtoolnameâ€ť.. â€śtoolnameâ€ť..)
``````

I have already tried this but I donâ€™t know how to put it all into one variable, because I want to have all the random parts I chose under a variable

I donâ€™t try ink you are able to do that because they are all different parts, Iâ€™m not a programmer so

``````local t = {"test1", "test2", "test3", "test4", "test5"}

local choiceIndex = math.random(1, #t)
local choice = t[choiceIndex]

print(choice)
``````

You can make a loop after this to choose 3 values.

that only gets one valueâ€¦ lol rip

You would have to do `for 1,3 do` Before that

Then create a loop to give you 3 values.

how would I do that sorry im not that good with for loops tbh rip

``````local t = {"test1", "test2", "test3", "test4", "test5"}

for i = 1, 3 do
local choiceIndex = math.random(1, #t)
local choice = t[choiceIndex]
print(choice)
end
``````

but what if it chooses the same value? I want three different random values

This should doâ€¦

``````local function getItemsFromTable(t: {[any]: any}, n: number): {[any]: any}
n = n or 1
n = n < #t and n or #t
t = table.clone(t)

local _t = {}

for i = 1, n do
local v = table.remove(t, math.random(1, #t))

table.insert(_t, v)
end

return _t
end

local t = {1, true, "Hello, World!", {}, "something else", false}

print(getItemsFromTable(t, 3))
``````
``````local Partsfolder = game.Workspace.Folder -- Put the folder location here

local RandomParts = {} -- Table for storing the random parts

for i = 1, 3 do -- Get 3 random parts
table.insert(RandomParts, Partsfolder:GetChildren()[math.random(1, #Partsfolder:GetChildren())])
-- Insert each random part into the table RandomParts
end
``````

You can do this (if youâ€™re wondering how I can use table.remove like that as the object to insert into the table, fun fact that table.remove returns the instance youâ€™re removing ):

``````local randomPartAmount = 3

local folderChildren = FOLDER:GetChildren()
local selectedChildren = {}

-- For a sanity check, we can just set randomPartAmount to the amount of parts in the folder, hence the script should never break, hence there's no need for a check in the for loop below.
if randomPartAmount > #folderChildren then
randomPartAmount = #folderChildren
end

-- For loop iterating from 1 to the randomPartAmount (3 in this case).
for i = 1, randomPartAmount  do

-- Get a random index between 1 and the length of the children table, which should be the number of parts in the folder.
local randomIndex = Random.new():NextInteger(1,#folderChildren)

-- Now, insert into the selectedChildren table the part from 'folderChildren' at a random index of the table.
table.insert(selectedChildren, table.remove(folderChildren, randomIndex))
end

-- Print it out to see the results.
print(unpack(selectedChildren))
``````
1 Like
``````local t = {"test1", "test2", "test3", "test4", "test5"}
local chosen = {}

for i = 1, 3 do
local choiceIndex = math.random(1, #t)
local choice = t[choiceIndex]
if not table.find(chosen, choice) then
table.insert(chosen, choice)
end
end
``````

Btw, in your script if the part was already in the table then it would just skip and the for loop would continue, meaning thereâ€™s a chance you get less than 3 parts in the table.

@xmthlâ€™s script should be the solution here Iâ€™m pretty sure

1 Like

I just clocked onto that, @xmthlâ€™s script is much better than mine. I wrote it on phone.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.