Help with table manipulating

What I’m trying to do is loop through this model: Screenshot 2021-06-12 135828 and end up with a table like this

local desiredTable = {Side1, Side2, Side3, Side4, Side5, Side6}

I would just use :GetChildren but I need it in order and for some reason it mixes it up and gets it in the wrong order. Here is the code:

		local SidePositions = {0,0,0,0,0,0}
		for key, Side in pairs(Die:GetChildren()) do
			if Side.Name ~= "Body" and Side:IsA("Part") then
				local newString = Side.Name
				local name = string.gsub(newString, "Side", " ")
				table.remove(SidePositions, tonumber(name))
				table.insert(SidePositions, tonumber(name), Side)

You would have to iterate through the children with a standard for loop.

local sides = {}
for i = 1,#die:GetChildren() do
    sides[#sides + 1] = die:FindFirstChild('Size'..i)
    -- we don't have to check if the part is a basepart as it would be equal to sides[i + 1] = nil which is basically the same as doing nothing
print(sides) --[[--> {
    [1] = Side1,
    [2] = Side2,
    [3] = Side3 etc...

You can just get the number out of each childs name, and put it in the table. For example:

local sides = {}
for _,v in pairs(Parent:GetChildren()) do
    local num = string.format(v.Name,%c)
    if num then

Yay! It works! But where is another problem. I actually want to make the table full of each side’s Y position but when I try this

SidePositions[#SidePositions + 1] = Die:FindFirstChild('Side'..i).Position.Y

It hits me with this error:
Workspace.TestMap.Side1.CharacterSpawn.Script:140: attempt to index nil with 'Position'
but the side object is clearly not nil because I can access it in the table later in the script if I don’t add the Position.Y. Any suggestions?

Oh yeah, that’s happening because :FindFirstChild is returning nil as it hasn’t found a child called “Side”…i (which could technically be like 10 if you have more than just the parts).

So you should do this instead:

local sides = {}
for i = 1,#die:GetChildren() do
    local foundChild = die:FindFirstChild('Size'..i)
    if foundChild and foundChild:IsA('BasePart') then
        sides[#sides + 1] = foundChild.Position.Y
1 Like