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)
end)
for i, v in ipairs(objects) do
v.LayoutOrder = i
v.Parent = YourGui
end
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.
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.
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.
local objects = frame.Text:GetChildren()
table.sort(objects, function(a,b)
return tonumber(a.Parent.Cost.Text) < tonumber(b.Parent.Cost.Text)
end)
for i, v in ipairs(objects) do
v.LayoutOrder = i
v.Parent.Parent = frame.Parent
end
However, the order now is:
50,75,0,1000
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
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.
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.