Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I Make A Server Leaderboard?

I'm trying to make a server leaderboard but don't know how to sort the players by how much money they have. I have the basic functionality scripted, all I need is just the sorting and moving of positions when a player passes another player.

local replicatedStorage = game:GetService("ReplicatedStorage")

local Players = game:GetService("Players")

local Template = replicatedStorage:WaitForChild("Template")

Players.PlayerAdded:Connect(function(Plr)
	local newTemplate = Template:Clone()
	newTemplate.Name = Plr.Name
	newTemplate.Position = UDim2.new(0, 0, newTemplate.Position.Y.Scale + (0.1 * #script.Parent.GUI.Holder:GetChildren()), 0)
	newTemplate.Parent = script.Parent.GUI.Holder
	
	newTemplate.Rank.Text = "#0"
	newTemplate.Player.Text = Plr.Name
	wait(2)
	newTemplate.Cash.Text = Plr.leaderstats.Cash.Value
end)

Players.PlayerRemoving:Connect(function(Plr)
	for i, v in pairs(script.Parent.GUI.Holder:GetChildren()) do
		if v.Name == Plr.Name then
			v:Destroy()
		end
	end
end)

1 answer

You can use the table.sort function. It provides a predication argument that allows us to supply a method of bias to how the table is sorted.

local AllPlayersInLeaderboard = --// Get a table of everyone from your board.

table.sort(AllPlayersInLeaderboard, function(a,b)
	a.leaderstats.Cash.Value > b.leaderstats.Cash.Value
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.