How could I organize GetChildren()?

Hello, these functions create a frame in the shop for each child inside a skins folder although it’s never the same each time due to it being GetChildren(), how could I make it organize the children in a specific order? Any help is appreciated.

function AddSkins(Data)
	for i, v in ipairs(Data) do
		local Frame = CreateFrame(v.Name, v.Cost.Value, v.Image.Value)
	end
end

SendData.OnClientEvent:Connect(function()
	AddSkins(SkinsList)
end)
1 Like

You would have to copy the children to a new array in the desired order

For example you could alphabetize them:

local allFrames = game.Workspace.Folder:GetChildren()
local organizedList = {}
local candidate = {Name = "zzzzz"}
local lastOrganizedItem = {Name = "aaaa"}

-- While you have not yet organized all of them...
while #organizedList < #allFrames do
	local candidate = {Name = "zzzzz"}
	-- Go through all children in the disorganized list of children
	for _, child in pairs(allFrames)  do
		-- If the child you're currently looking at is lower alphabetically than the previous candidate,
		-- but higher alphabetically than the last one that was organized,
		-- this is the next best candidate (and will be added to the array next)
		if child.Name:lower() < candidate.Name:lower() and child.Name:lower() > lastOrganizedItem.Name:lower() then
			candidate = child
		end
	end
	-- The entire disorganized list has been searched and you've found the next candidate in alphabetical order
	--  Add it to the organized list
	table.insert(organizedList, candidate)
	lastOrganizedItem = candidate
end
-- organizedList now contains a list of all the children organized in alphabetical order
5 Likes

If you want them organized you should try to make a table yourself.

I think there is another way but I have no idea what it is

table.sort could be useful aswell, you can return children in a table in an order such that a>b or b>a, etc.

3 Likes

does it work for GetChildren() too?

That’s because dictionaries always have random order. To fix this, all you need is a table with all the items you want to display in the shop, in the order you wish.

local orderedItems = {'Item1', 'Item2', 'Item3'}

for _, v in ipairs(orderedItems) do
  local Item = Data[v]
  -- do something with the item...
end
2 Likes

Yes.

local myTab = game.Workspace.MyParts:GetChildren()

table.sort(myTab,function(a,b)
	return tonumber(a.Name) < tonumber(b.Name)
end)



for _,item in pairs(myTab) do
	print(item)
end

This is a very simple example :
image
image

3 Likes

Without that table.sort, it’d have look like this -

image

1 Like

table.sort(Data, function(a, b) return a.Name < b.Name end)

If you wanted to sort the items alphabetically.

3 Likes