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

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

Asked by 3 years ago

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

0
What do you mean by "randomize objects in folders" User#30567 0 — 3y

2 answers

Log in to vote
1
Answered by
gskw 1046 Moderation Voter
3 years ago

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
Ad
Log in to vote
0
Answered by
dcaa77 0
3 years ago

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

Answer this question