How to order frames based off of a value inside of it?

So, after I call a remote function, I make a frame. I then, set an object in a storage to go inside the viewportframe.

But when I play, it is ordered (based off of the values) like this:


whereas I want them to be


Is there a way to do this?

I have looked into GUI Objects, tables, but not sure how to order it.

If you need any resources, then feel free to ask.

Can you show what you mean with a screenshot?


Sure, I’ll send them:

(scribbled out since new update for my game)

The numbers I provided were an example, but the list above goes:


whereas I want them to be


If it’s a UIGridLayout or UIListLayout, change the way they order to be LayoutOrder rather than by Name.

Then, in your script, get all the objects and do table.sort to sort them by their text as a number, e.g.:

local objects = game.ReplicatedStorage.GuiObjects:GetChildren()
table.sort(objects, function(a,b)
    return tonumber(a.Text) < tonumber(b.Text)
for i, v in ipairs(objects) do
    v.LayoutOrder = i
    v.Parent = YourGui

Alright, I tested it out, and here is the error in the output:

attempt to compare number and nil
I am looking into it.

The issue will be no text in one of your guiobjects in the sort function. Make sure all of them have a number in.

1 Like

Alright, did what you said, but it detects a and b as ImageButtons.

Should I do an ‘if’ statement? Or?

I don’t know what your hierarchy is like. If a and b are buttons that have a TextLabel inside, then in that comparison function you need to make it reference the text you are interested in.

Whether that is a.Text or a.TextLabel.Text or wherever that text is located - that’s what it needs to get hold of.

1 Like

Alright, sorry for the delay. I have done as requested, but the sorting is still off.

It goes


I am looking into ways of fixing it but, any advice from here?

Are you using UIListLayout or UIGridLayout and have you set its SortOrder property to LayoutOrder?

1 Like

I am using a GridLayout currently, but if that is wrong, I will change.

And yes, LayoutOrder has been set.


Any further advice on how to work this? If you need photos, please say!

Check that the buttons are being given the correct LayoutOrder values by running the game in studio and checking in your PlayerGui.

If they are being given the wrong LayoutOrder then you’ll need to work out if perhaps it’s applying the layout order to the wrong object, such as a child object when it needs to be applied to the parent perhaps.

1 Like

i always use GUI contraints like the UILIstLayout to sort my items. Then you just need to append a number or something int the name of the GUI element and let the UILIstLayout object do the sorting for you. No need for any fancy sort functions, plus it might even be faster, letting the UIConstraint do that heavy lifting.

My script is this:

		local objects = frame.Text:GetChildren()
					table.sort(objects, function(a,b)
			  		return tonumber(a.Parent.Cost.Text) < tonumber(b.Parent.Cost.Text)
		for i, v in ipairs(objects) do
    				v.LayoutOrder = i
				    v.Parent.Parent = frame.Parent

However, the order now is:


I am a little stumped by this, as I haven’t come across these sort of scripts before.

Can you send a screenshot of the hierarchy of these buttons and text labels as the sort function implies the LayoutOrder might be being applied in the wrong place

1 Like


This what you mean?

This doesn’t seem to match your line where it says frame.Text:GetChildren()

Can you either provide the full script, or point at what frame is going to refer to and what the children look like?

It also might be easier to understand what happens to the buttons if you show a screenshot of that once the script has run - i.e. in Studio in Run mode.

1 Like

Oops, yeah. I deleted the part of text:GetChildren() Sorry for the misunderstanding.

is now frame:GetChildren()

I have also come to realise that when the player buys the character, the text changes. Would this effect it?

Screenshot (83)

So the LayoutOrder needs to be applied to whatever is at the same level as your UIGridLayout. I can’t see the grid layout anywhere on your screenshots which is a bit strange, so I can’t really help you identify which object to apply the LayoutOrder to.

And yes you will need to redo the LayoutOrder values when the text changes.

1 Like