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)
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
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
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