Get random child of a folder

I want a script to select a random child of a folder. How do I do this?

local serverstorage = game:GetService("ServerStorage")
local folder = serverstorage.Folder
local selecteditem = -- the chosen part inside the folder
3 Likes
local serverstorage = game:GetService("ServerStorage")
local folder = serverstorage.Folder:GetChildren()
local selecteditem = folder[math.random(1, #folder)]

Should work like this, tell me if there are errors.

17 Likes

Hey! I tried the script you did, but it did not work for me.
My script:

local randomitem = 0
randomitem = ServerStorage.Blocks[math.random(1, #ServerStorage.Blocks:GetChildren())]

My error is: “26 is not a valid member”, it seems to generate a Number instead of the child’s name. Do you know how I can fix that?

Fixed script

local randomitem
randomitem = ServerStorage.Blocks:GetChildren()[math.random(1, #ServerStorage.Blocks:GetChildren())]

Thank you so much, yeah I see I missed the :GetChildren() now.

1 Like