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

Can we do math.random for folders? If we can how?

I mean for example there is folder and the folder has things in it how do we randomize them?

1 comment

0

What do you mean by "randomize objects in folders"

2 answers

✓ Accepted answer

Given that the folder variable holds the Folder object, we can get its children in an array, pick a random index that is within the bounds of the array, and pick the child corresponding to that index:

local children = folder:GetChildren()
local count = #children
local random_index = math.random(1, count) -- random integer from 1 to count (inclusive)
local random_child = children[random_index]
-- random_child will now be one of the Folder's children, picked randomly

Good question! Here's an example of it! I use this all the time for things like randomly generated levels and stuff. Here's a video with a good example of it being used: https://youtu.be/_IbCnucSWjU

local Children = Folder:GetChildren() -- Gets the children of the folder
local ChosenChild = Children[math.random(1,#Children)] -- Chooses a child from the folder
print(ChosenChild.Name) -- Prints the name of the chosen child

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