Getting a random Descendant?

I’m attempting to select a random Shade from within my GUI - As you can see, the UI is split into different sections:

  • SkinCol
  • HairStyle
  • OutfitStyle

Within these is a frame enlisted ‘Free’ with other frames listing the Shades & so forth.
How can I go about choosing a Descendant at Random for the corresponding randomizer?
i.e. random Skin is a random shade from SkinCol, HairStyle is random from HairStyle etc.

1 Like

Let’s say you want a random hair color from free and get a shade:

local FreeHairColors = charEditorUI.UI.HairStyle.ColChart.List.Free:GetChildren()
-- I assume the UIListLayout shouldn't be chosen?
function GetRandomShade()
local Shades = {}
for i,hairColor in pairs (FreeHairColors) do
 if hairColor ~= UIListLayout then
for i,Shade in pairs (hairColor:GetChildren()) do
Shades[#shades + 1] = Shade
return Shades[math.random(1,#Shades)] 

local RandomShade = GetRandomShade()

I can’t test this. I’m also a human that can make mistakes.

Iterate over the descendants and collect all descendants that are of type “ImageLabel” and then pickack a random one.

local shades = {} --// store the shades 

for i, descendant in pairs(free:GetDescendants()) do --// iterate over descendants
    if (descendant.Name ~= "Shade") then continue end --// make sure the code below this is only ran if their name is shade

    shades[#shades + 1] = descendant --// push descendant into shades array

local function getRandomShade() --// function to get a random shade
    return shades[math.random(1, #shades)] --// you can get a random element from an array by indexing the array with a number between 1 and the length of the array

local randomShade = getRandomShade() --// finally call the function to get a random one
1 Like

You’re using :GetChildren while you should be using :GetDescendants as the shades are Descendants of Free

Also you forgot some () infront of your function, ChoseRandomHairColor.

I never used :GetDescendants() before. I understand the concept of it now. Looks like I just wrote some extra steps. :joy: