:GetChildren() Help

I’m making a game and in ReplicatedStorage is a folder full of hats which are parented to a value of which order they should be pasted into a GUI.

Although when I run this code:

for i,v in pairs(game.ReplicatedStorage.Hats:GetChildren()) do
   local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("Frame"):Clone()
   gui.Name = v.HatName.Value
   gui.Parent = script.Parent
   local name = v.HatName.Value
   local vv = v[name].ImageLabel
   vv.Size = UDim2.new(0.822, 0,0.815, 0)
   vv.Parent = gui
   vv.Position = UDim2.new(0.087, 0,0.086, 0)
   local a = Instance.new("UIAspectRatioConstraint")
   a.AspectRatio = 1.085
   a.Parent = vv
   gui.TextLabel.Text = v.HatName.Value
   gui.Price.Value = v.Price.Value
   gui.Multiplier.Value = v.Level.Value
end

It will get all hats and duplicate the frame and add all necessary details but in a random order. How can I make it do it in order according to the Numbers?

image

1 Like

I don’t know the order of the instances in a table returned by :GetChildren, but for your case you should use a numeric for loop since order really matters to you in this case.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local hats_instance = ReplicatedStorage.Hats
local hats = hats_instance:GetChildren()

for i = 1, #hats do
    local current_hat = hats_instance[i]
end

Amd now you have order.

1 Like

If you’re using a grid or list, just set the LayoutOrder property to some number. Lower numbers go first.

1 Like

Thank you so so much! This has really helped! I don’t know why I didn’t think of this ahaha :slight_smile: