How Would I Sort Labels by a NumberValue Inside It?

I have a script that clones a label into a player’s gui and in the label, there is a NumberValue | Roblox Creator Documentation with the value as a number. I want them to be laid out by that NumberValue’s value inside it. For example, if one NumberValue | Roblox Creator Documentation had 15 as the value and another NumberValue | Roblox Creator Documentation had 255 as the value, I’d want the 255 one to be on top of the 15 one. There is a UIListLayout in the frame they’re put in.

You would just use this.
LayoutOrder - does everything you wanted
image

1 Like

Could you help me? I’m a bit confused. This is my local script and GUI’s:
Screenshot 2021-05-07 at 09.01.08

Script
local labels = {}

function Update()
	for _,Player in pairs(game.Players:GetChildren()) do
		local makelabel = true
		for Number,Label in pairs(labels) do
			if Label[1] == Player.Name then
				makelabel = false
				break
			end
		end
		if makelabel and Player.leaderstats:FindFirstChild("Points") then
			local label = script.Sam:Clone()
			label.PlayerNameLabel.Text = Player.Name
			label.StatValueLabel.Text = 0
			label.RankNameLabel.Text = Player:GetRoleInGroup(7216249)
			label.RankNumberValue.Value = Player:GetRankInGroup(7216249)
			if Player:GetRankInGroup(7216249) >= 9 then
				label["MR/HRLabel"].Visible = true
			end
			label.Visible = true
			label.Parent = script.Parent.Holder
			label.MouseButton1Click:connect(function()
				print("Has Clicked "..Player.Name)
				label.Window.Visible = not label.Window.Visible
			end)
			table.insert(labels,{Player.Name,label,0})
		end
	end
	if labels ~= {} then
		for i,v in pairs(labels) do
			if game.Players[v[1]] == nil then
				v[2]:Destroy()
				v = nil
			else
				v[3] = "⭐"..game.Players[v[1]].leaderstats.Points.Value
			end
		end
		table.sort(labels,function(ValueA,ValueB)
			if ValueA and ValueB then
				return ValueA[3] > ValueB[3]
			end
			return 1 > 2
		end)
		for Pos,Label in pairs(labels) do
			Label[2].LayoutOrder = Pos
			Label[2].StatValueLabel.Text = Label[3]
		end
	end
end

while true do
	Update()
	wait(60)
end!

There’s a lot going on it would help me. Help you. If you explained what you needed help with specifically so I can narrow down what I’m looking for or suppose to be looking for.

This is a script for my custom leaderboard. The RankNumberValue has the rank of the player in my group. So it will be the number 255 for me because I’m the owner. But if it was someone who wasn’t in my group, it would be 0.

I’m trying to have the higher ranked players at the top and the lower ranked players at the bottom.

Try changing your script into this:

local labels = {}

function Update()
	for _,Player in pairs(game.Players:GetChildren()) do
		local makelabel = true
		for Number,Label in pairs(labels) do
			if Label[1] == Player.Name then
				makelabel = false
				break
			end
		end
		if makelabel and Player.leaderstats:FindFirstChild("Points") then
			local label = script.Sam:Clone()
			label.PlayerNameLabel.Text = Player.Name
			label.StatValueLabel.Text = 0
			label.RankNameLabel.Text = Player:GetRoleInGroup(7216249)
			label.RankNumberValue.Value = Player:GetRankInGroup(7216249)
            label.LayoutOrder = label.RankNumberValue.Value
			if Player:GetRankInGroup(7216249) >= 9 then
				label["MR/HRLabel"].Visible = true
			end
			label.Visible = true
			label.Parent = script.Parent.Holder
			label.MouseButton1Click:connect(function()
				print("Has Clicked "..Player.Name)
				label.Window.Visible = not label.Window.Visible
			end)
			table.insert(labels,{Player.Name,label,0})
		end
	end
	if labels ~= {} then
		for i,v in pairs(labels) do
			if game.Players[v[1]] == nil then
				v[2]:Destroy()
				v = nil
			else
				v[3] = "⭐"..game.Players[v[1]].leaderstats.Points.Value
			end
		end
		table.sort(labels,function(ValueA,ValueB)
			if ValueA and ValueB then
				return ValueA[3] > ValueB[3]
			end
			return 1 > 2
		end)
		for Pos,Label in pairs(labels) do
			Label[2].LayoutOrder = Label[2].RankNumberValue.Value
			Label[2].StatValueLabel.Text = Label[3]
		end
	end
end

while true do
	Update()
	wait(60)
end!

Let me know if it helped.

Thank you this helped but here are a little few changes I had to make. The first was an obvious accidental mistake, you put an ! at the end. And since layout order goes ascending, I had to change it to

label.RankNumberValue.Value = -Player:GetRankInGroup(7216249)
label.LayoutOrder = label.RankNumberValue.Value

to make it descending.