How to order my custom leaderboard gui from highest to lowest?

So I have a custom leaderboard gui. All of the players frames are within a frame with a UIListLayout gui element. How would I be able to order the players to be from highest amount of kills to the lowest amount of kills, like shown below.

Local Script
--= Roblox Services =--
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")

--= Object Refrences =--
local HideButton = script.Parent.HideButton

--= Internal Functions =--
local function addPlayerToLeaderboard(plr)
	
	local templateFrame = script.Template:Clone()
	templateFrame.Username.Text = plr.Name
	templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
	templateFrame.Kills.Text = plr:WaitForChild("leaderstats").Kills.Value
	templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
	
	templateFrame.Parent = script.Parent.PlayerContainer
	templateFrame.Visible = true
	
--	templateFrame.Name = plr.Name
	
	for _,leaderstat in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
		leaderstat:GetPropertyChangedSignal("Value"):Connect(function()
			templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
			templateFrame.Kills.Text = plr:WaitForChild("leaderstats").Kills.Value
			templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
		end)
	end
end

local function handleVisibility()
	if HideButton.Text == "Hide" then
		HideButton.Text = "Show"
		
		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.955, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0,0.051, 0)}):Play()
		
		task.wait(0.05)
	elseif HideButton.Text == "Show" then
		HideButton.Text = "Hide"
		
		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.79, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0,0.051, 0)}):Play()
		
		task.wait(0.05)
	end
end

--= Initializers =--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

for _, v in pairs(game.Players:GetChildren()) do
	addPlayerToLeaderboard(v)
end

Players.PlayerAdded:Connect(function(plr)
	addPlayerToLeaderboard(plr)
end)

HideButton.MouseButton1Click:Connect(handleVisibility)

Players.PlayerRemoving:Connect(function(plr)
	for i, v in pairs(script.Parent.PlayerContainer:GetChildren()) do
		if v and v.Name == plr.Name then
			v:Remove()
			break
		end
	end
end)

How I wan’t it to look: (ordered by highest amount of kills on top)

image

How it currently works:

image

Note: you have to have UIGridLayout or UIListLayout (i haven’t tested list out rn), Sort Order has to be layout order

local function sortplayerByKills(players)
	table.sort(players:GetPlayers(), function (p1, p2)
		return p1:WaitForChild("leaderstats").Kills.Value > p2:WaitForChild("leaderstats").Kills.Value
	end)
end

--= Roblox Services =--
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")

--= Object Refrences =--
local HideButton = script.Parent.HideButton

--= Internal Functions =--
local function addPlayerToLeaderboard(plr)
	
	local templateFrame = script.Template:Clone()
	templateFrame.Username.Text = plr.Name
	templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
	templateFrame.Kills.Text = plr:WaitForChild("leaderstats").Kills.Value
	templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
	
	templateFrame.Parent = script.Parent.PlayerContainer
	templateFrame.Visible = true
	
--	templateFrame.Name = plr.Name
	
	for _,leaderstat in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
		leaderstat:GetPropertyChangedSignal("Value"):Connect(function()
			templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
			templateFrame.Kills.Text = plr:WaitForChild("leaderstats").Kills.Value
			templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
		end)
	end
end

local function handleVisibility()
	if HideButton.Text == "Hide" then
		HideButton.Text = "Show"
		
		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.955, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0,0.051, 0)}):Play()
		
		task.wait(0.05)
	elseif HideButton.Text == "Show" then
		HideButton.Text = "Hide"
		
		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.79, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0,0.051, 0)}):Play()
		
		task.wait(0.05)
	end
end

--= Initializers =--
local playersSorted = sortplayerByKills(Players)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

for _, v in ipairs(sortPlayerByKills) do
	addPlayerToLeaderboard(v)
end

Players.PlayerAdded:Connect(function(plr)
	addPlayerToLeaderboard(plr)
end)

HideButton.MouseButton1Click:Connect(handleVisibility)

Players.PlayerRemoving:Connect(function(plr)
	for i, v in pairs(script.Parent.PlayerContainer:GetChildren()) do
		if v and v.Name == plr.Name then
			v:Remove()
			break
		end
	end
end)

for player added you would reset the ui, then call the function again to include the new player in sort, and then display the ui.

--= Roblox Services =--
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")

local array = {}

--= Object Refrences =--
local HideButton = script.Parent.HideButton

--= Internal Functions =--
local function addPlayerToLeaderboard(plr)

	local templateFrame = script.Template:Clone()
	templateFrame.Username.Text = plr.Name
	templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
	
	local kills = plr:WaitForChild("leaderstats").Kills.Value
	
	templateFrame.Kills.Text = kills
	templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value

	templateFrame.Parent = script.Parent.PlayerContainer
	templateFrame.Visible = true

	--	templateFrame.Name = plr.Name

	for _,leaderstat in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
		leaderstat:GetPropertyChangedSignal("Value"):Connect(function()
			templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
			templateFrame.Kills.Text = plr:WaitForChild("leaderstats").Kills.Value
			templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
		end)
	end
	
	table.insert(array, {Template = templateFrame, Kills = kills})
end

local function handleVisibility()
	if HideButton.Text == "Hide" then
		HideButton.Text = "Show"

		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.955, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0,0.051, 0)}):Play()

		task.wait(0.05)
	elseif HideButton.Text == "Show" then
		HideButton.Text = "Hide"

		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.79, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0,0.051, 0)}):Play()

		task.wait(0.05)
	end
end

local function sort()
	local clonedArray = table.clone(array)
	table.sort(clonedArray, function(a, b)
		return a.Kills > b.Kills
	end)
	for i, v in clonedArray do
		v.Template.LayoutOrder = i
	end
end

--= Initializers =--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

for _, v in pairs(game.Players:GetChildren()) do
	addPlayerToLeaderboard(v)
	sort()
end

Players.PlayerAdded:Connect(function(plr)
	addPlayerToLeaderboard(plr)
	sort()
end)

HideButton.MouseButton1Click:Connect(handleVisibility)

Players.PlayerRemoving:Connect(function(plr)
	for i, v in pairs(script.Parent.PlayerContainer:GetChildren()) do
		if v and v.Name == plr.Name then
			v:Remove()
			break
		end
	end
end)
1 Like

this works but the only flaw is that it doesnt update when a leaderstat(Kills) updates its value.

Oh, sorry I forgot about that. Here:

--= Roblox Services =--
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")

local array = {}

--= Object Refrences =--
local HideButton = script.Parent.HideButton

--= Internal Functions =--

local function sort()
	local clonedArray = table.clone(array)
	table.sort(clonedArray, function(a, b)
		return a.Kills > b.Kills
	end)
	for i, v in clonedArray do
		v.Template.LayoutOrder = i
	end
end

local function addPlayerToLeaderboard(plr)

	local templateFrame = script.Template:Clone()
	templateFrame.Username.Text = plr.Name
	templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value

	local kills = plr:WaitForChild("leaderstats").Kills.Value

	templateFrame.Kills.Text = kills
	templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value

	templateFrame.Parent = script.Parent.PlayerContainer
	templateFrame.Visible = true

	--	templateFrame.Name = plr.Name

	for _,leaderstat in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
		leaderstat:GetPropertyChangedSignal("Value"):Connect(function()
			templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
			templateFrame.Kills.Text = plr:WaitForChild("leaderstats").Kills.Value
			templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
			
			sort()
		end)
	end

	table.insert(array, {Template = templateFrame, Kills = kills})
end

local function handleVisibility()
	if HideButton.Text == "Hide" then
		HideButton.Text = "Show"

		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.955, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0,0.051, 0)}):Play()

		task.wait(0.05)
	elseif HideButton.Text == "Show" then
		HideButton.Text = "Hide"

		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.79, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0,0.051, 0)}):Play()

		task.wait(0.05)
	end
end

--= Initializers =--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

for _, v in pairs(game.Players:GetChildren()) do
	addPlayerToLeaderboard(v)
	sort()
end

Players.PlayerAdded:Connect(function(plr)
	addPlayerToLeaderboard(plr)
	sort()
end)

HideButton.MouseButton1Click:Connect(handleVisibility)

Players.PlayerRemoving:Connect(function(plr)
	for i, v in pairs(script.Parent.PlayerContainer:GetChildren()) do
		if v and v.Name == plr.Name then
			v:Remove()
			break
		end
	end
end)

this doesnt appear to be working since ive tried it before. I noticed that when the player resets, the leaderboard gets automatically sorted and ordered in the right way.

Whoops I didn’t update the kills on the array, this might work now…

--= Roblox Services =--
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")

local array = {}

--= Object Refrences =--
local HideButton = script.Parent.HideButton

--= Internal Functions =--

local function sort()
	local clonedArray = table.clone(array)
	table.sort(clonedArray, function(a, b)
		return a.Kills > b.Kills
	end)
	for i, v in clonedArray do
		v.Template.LayoutOrder = i
	end
end

local function addPlayerToLeaderboard(plr)

	local templateFrame = script.Template:Clone()
	templateFrame.Username.Text = plr.Name
	templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value

	local kills = plr:WaitForChild("leaderstats").Kills.Value

	templateFrame.Kills.Text = kills
	templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value

	templateFrame.Parent = script.Parent.PlayerContainer
	templateFrame.Visible = true

	--	templateFrame.Name = plr.Name
	
	local nestedArray = {Template = templateFrame, Kills = kills}
	
	for _,leaderstat in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
		leaderstat:GetPropertyChangedSignal("Value"):Connect(function()
			templateFrame.Level.Text = plr:WaitForChild("leaderstats").Level.Value
			
			local kills = plr:WaitForChild("leaderstats").Kills.Value
			
			templateFrame.Kills.Text = kills
			templateFrame.Deaths.Text = plr:WaitForChild("leaderstats").Deaths.Value
			
			nestedArray.Kills = kills

			sort()
		end)
	end

	table.insert(array, nestedArray)
end

local function handleVisibility()
	if HideButton.Text == "Hide" then
		HideButton.Text = "Show"

		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.955, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(1.1, 0,0.051, 0)}):Play()

		task.wait(0.05)
	elseif HideButton.Text == "Show" then
		HideButton.Text = "Hide"

		TweenService:Create(HideButton, TweenInfo.new(0.4), {Position = UDim2.new(0.79, 0,0.013, 0)}):Play()
		TweenService:Create(script.Parent.Header, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0, 0.011, 0)}):Play()
		TweenService:Create(script.Parent.PlayerContainer, TweenInfo.new(0.4), {Position = UDim2.new(0.825, 0,0.051, 0)}):Play()

		task.wait(0.05)
	end
end

--= Initializers =--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

for _, v in pairs(game.Players:GetChildren()) do
	addPlayerToLeaderboard(v)
	sort()
end

Players.PlayerAdded:Connect(function(plr)
	addPlayerToLeaderboard(plr)
	sort()
end)

HideButton.MouseButton1Click:Connect(handleVisibility)

Players.PlayerRemoving:Connect(function(plr)
	for i, v in pairs(script.Parent.PlayerContainer:GetChildren()) do
		if v and v.Name == plr.Name then
			v:Remove()
			break
		end
	end
end)
2 Likes

yup works perfectly, thank you for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.