Selecting multiple random objects from folder

So I’ve tried looking everywhere but I can’t seem to find the exact thing I’m looking for.

Basically I need to select multiple random objects inside a folder and then enable things inside the objects using i,v in pairs loop for example. I’m able to select the random object but I can’t seem to figure out how to select multiple of them and then use them in pairs if that makes sense. Is anyone able to point me in the direction of understanding it.

1 Like

Make a for loop to choose random children and then use ipairs on the table of randomly seleced children

local Folder = --your folder
local FolderChildren = Folder:GetChildren()
local chosen = {} -- where we will be storing the randomly selected objects

local MAX_OBJECTS = 3

for _ = 1, MAX_OBJECTS do
   local index = math.random(1, #FolderChildren)
   local obj = FolderChildren[index]

   if not table.find(chosen, obj) then
      table.insert(chosen, obj)
   else
      repeat 
         index = math.random(1, #FolderChildren)
         obj = FolderChildren[index]
      until not table.find(chosen, obj)

      table.insert(chosen, obj)
   end
end

Edit: Fixed a bug where the new random object picked in the repeat loop doesnt get added to the chosen. And also added a variable to determine the maximum amount of objects to choose.

4 Likes