Start building

How to make a leaderboard in Roblox: leaderstats and a global top 10

Show Coins and Wins in the player list with one server script, then put the best players of all time on a board in your lobby.

A global Top Coins leaderboard in a Roblox lobby, with a winners' podium for the top three players below it.
On this page
  1. How leaderstats works
  2. Write the leaderstats script
  3. Change stats only on the server
  4. Save the stats between sessions
  5. Save scores to an OrderedDataStore
  6. Show the top 10 on a board in your lobby
  7. Stay inside the request limits
  8. Fix common leaderboard problems
  9. Questions

A leaderstats script is a server Script that puts a Folder named leaderstats inside each player, with value objects such as Coins and Wins in it. Roblox turns that folder into the leaderboard in the player list, so you write no UI at all. For an all-time global top 10, you also save every player's score to an OrderedDataStore and draw the best ten on a board in your lobby.

How leaderstats works

Roblox checks every Player object for a child named exactly leaderstats, all lowercase. Each value object inside it becomes a column in the player list in the top-right corner, with the object's name as the header. The first stat you add is the one the list sorts players by, highest first.

Three value types are worth knowing:

Value typeHoldsGood for
IntValueWhole numbersCoins, Wins, Kills, Stage
NumberValueDecimalsBest time in seconds, a multiplier
StringValueTextA rank or title, like "Pro"

Two rules catch most beginners. The folder goes inside the Player, the object under Players in the Explorer, not inside the character model in Workspace. And a server Script creates it, so every player sees the same numbers. Leaderstats also don't save on their own: when a player leaves, the folder goes with them.

Write the leaderstats script

  1. In the Explorer, hover over ServerScriptService, click the + button and insert a Script.
  2. Rename it Leaderstats and replace its code with this:
ScriptServerScriptService
-- Script in ServerScriptService: gives every player a leaderstats folder with Coins and Wins
local Players = game:GetService("Players")

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	leaderstats.Parent = player
end

Players.PlayerAdded:Connect(onPlayerAdded)

-- Anyone who joined before this script started
for _, player in Players:GetPlayers() do
	onPlayerAdded(player)
end

Players.PlayerAdded runs onPlayerAdded once for each player who joins. The function builds the folder, adds two IntValues and parents the folder to the player last, so it arrives with both stats already inside. The loop at the bottom covers anyone who joined before the script started, which can happen in Studio.

Start a playtest and you should see Coins and Wins next to your name. Coins was added first, so the list ranks players by Coins. To rank by Wins, create it first.

Change stats only on the server

A change made in a LocalScript stays on that one player's screen. Nobody else sees it, the server ignores it, and an exploiter can change their own client however they like. So every change to a stat happens in a server Script, in response to something the server can check: a touch, a finished round, a purchase it has verified.

Here is a finish pad for the end of an obby. Touching it gives a win and 25 coins, with a short cooldown so one finish counts once.

Script
-- Script inside the finish pad Part: adds 1 Win and 25 Coins on touch
local Players = game:GetService("Players")

local pad = script.Parent
local cooldown = {} -- players who just won and have to wait

pad.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player or cooldown[player] then
		return
	end
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		return
	end

	cooldown[player] = true
	leaderstats.Wins.Value += 1
	leaderstats.Coins.Value += 25

	task.delay(5, function()
		cooldown[player] = nil
	end)
end)

Touched fires for every part that hits the pad, arms and legs included, so GetPlayerFromCharacter(hit.Parent) turns the touching part into a player, or nil for anything that is not a character. The cooldown table blocks that player for five seconds and task.delay clears them afterward. Most obbies also send the player back to the start at this point.

Save the stats between sessions

Right now everyone starts at zero each time they join. To keep their numbers, the server has to load them from a DataStore when a player joins and save them when they leave, with retries for the moments when a request fails.

The DataStore tutorial builds that as a ModuleScript called PlayerData. It loads each player's saved table, builds this same leaderstats folder from it and copies every change to those values back into the table. It saves on leave, every two minutes and when the server shuts down. To switch over:

  1. Add the PlayerData module and the small Script that starts it, both from that tutorial.
  2. Delete the Leaderstats script from this guide, or every player gets two folders.
  3. Keep the finish pad as it is. It still changes leaderstats.Wins.Value, and now that change is saved.

Save scores to an OrderedDataStore

The player list only shows who is in this server. A global leaderboard ranks everyone who ever played, and Roblox has a tool built for it: an OrderedDataStore, a data store that keeps one whole number per key and can return the keys sorted by value.

Every minute, the server writes each player's Wins under the key "Player_" .. player.UserId, the same key format the save system uses, then asks for the ten highest with GetSortedAsync(false, 10). The false means descending, so the biggest number comes first. New to pcall or tables? Roblox scripting for beginners covers both.

Add a Script to ServerScriptService named GlobalLeaderboard, starting with this:

ScriptServerScriptService
-- Script in ServerScriptService (GlobalLeaderboard), part 1: saves each player's Wins
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local STAT = "Wins"
local TOP_COUNT = 10
local REFRESH_SECONDS = 60 -- keep this at 60 or more

local ok, topStore = pcall(function()
	return DataStoreService:GetOrderedDataStore("Top" .. STAT .. "_v1")
end)
if not ok then
	warn("Global leaderboard is off: " .. tostring(topStore))
	return
end

local lastSent = {} -- [userId] = the score we last wrote

local function saveScore(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local stat = leaderstats and leaderstats:FindFirstChild(STAT)
	if not stat or player.UserId <= 0 then
		return -- no stats yet, or a Studio test player
	end
	local score = math.floor(stat.Value)
	if score <= 0 or lastSent[player.UserId] == score then
		return -- nothing new to write
	end
	local success, err = pcall(function()
		topStore:SetAsync("Player_" .. player.UserId, score)
	end)
	if success then
		lastSent[player.UserId] = score
	else
		warn("Could not save the score of " .. player.Name .. ": " .. tostring(err))
	end
end

Players.PlayerRemoving:Connect(function(player)
	saveScore(player)
	lastSent[player.UserId] = nil
end)

Every DataStore call can fail, and GetOrderedDataStore itself errors in a place that was never published, so each call sits inside pcall. A failed write is not retried on the spot, because the next refresh tries again a minute later. lastSent remembers the score last written for each player, so an unchanged score costs no request, and math.floor keeps the value whole, which ordered stores require. Test players in a multi-player Studio test have negative user IDs, so they are skipped and never reach your real board.

Plain SetAsync is fine here because this store only holds a copy of the score. The real save, done with UpdateAsync, lives in the PlayerData module.

Show the top 10 on a board in your lobby

Build the board once in Studio:

  1. Insert a Part into Workspace, name it LeaderboardBoard, turn on Anchored, size it to about 8 by 10 by 1 studs and place it in your lobby.
  2. Insert a SurfaceGui into the part. In Properties, set Face to the side players look at and SizingMode to PixelsPerStud.
  3. Inside the SurfaceGui, add a Frame named List with Size {1, 0},{1, 0}, and put a UIListLayout in it with SortOrder set to LayoutOrder.
  4. Add a second Frame to the SurfaceGui, not inside List, and name it RowTemplate. Set its Size to {1, 0},{0.1, 0}, turn off Visible, and give it three TextLabels side by side named Rank, PlayerName and Score.

Style the template however you like, because the script clones it once per row. Paste the second half below the first:

Script
-- GlobalLeaderboard Script, part 2: reads the top 10 and draws it (paste below part 1)
local surfaceGui = workspace:WaitForChild("LeaderboardBoard"):WaitForChild("SurfaceGui")
local list = surfaceGui:WaitForChild("List")
local template = surfaceGui:WaitForChild("RowTemplate")

local names = {} -- [userId] = username, fetched once each

local function getName(userId)
	if names[userId] then
		return names[userId]
	end
	local success, result = pcall(function()
		return Players:GetNameFromUserIdAsync(userId)
	end)
	if success then
		names[userId] = result
		return result
	end
	return "Player " .. tostring(userId)
end

local function refreshBoard()
	local success, entries = pcall(function()
		return topStore:GetSortedAsync(false, TOP_COUNT):GetCurrentPage()
	end)
	if not success then
		warn("Could not read the leaderboard: " .. tostring(entries))
		return
	end

	local rows = {}
	for rank, entry in ipairs(entries) do
		local userId = tonumber(string.sub(entry.key, 8)) -- cut off "Player_"
		table.insert(rows, { Rank = rank, Name = getName(userId), Score = entry.value })
	end

	for _, child in list:GetChildren() do
		if child:IsA("Frame") then
			child:Destroy()
		end
	end
	for _, info in rows do
		local row = template:Clone()
		row.LayoutOrder = info.Rank
		row.Rank.Text = "#" .. info.Rank
		row.PlayerName.Text = info.Name
		row.Score.Text = tostring(info.Score)
		row.Visible = true
		row.Parent = list
	end
end

while true do
	for _, player in Players:GetPlayers() do
		saveScore(player)
	end
	refreshBoard()
	task.wait(REFRESH_SECONDS)
end

GetSortedAsync returns the results in pages, and GetCurrentPage() gives the first page as a list of entries, each with a key and a value. The key is the "Player_" string, so the script cuts off its first seven characters to get the user ID back. Usernames come from a web request that can fail, so each one is fetched once inside pcall and kept in names. The rows are worked out before the old ones are removed, so the board never sits half empty while names load.

The loop at the bottom writes everyone's score, redraws the board, then waits a minute. The SurfaceGui lives in Workspace, so whatever the server draws on it shows for every player. If you would rather skip the setup, ask Revix to build the board in your lobby and wire both halves of the script.

Stay inside the request limits

Every server gets a budget for DataStore requests: a number of calls per minute for each kind of request, which grows with the number of players. Sorted reads like GetSortedAsync have a smaller budget than normal reads and writes, and writing the same key too often gets throttled. Go over and requests wait in a queue, then start to fail.

The board above stays well inside those limits because it:

  • reads the top 10 once per refresh, on the server, never once per player
  • writes a score only when it has changed
  • refreshes every 60 seconds, not every frame or every win

Limits change over time, so check the current numbers in the Creator Hub docs before you make the board refresh faster.

Fix common leaderboard problems

  • Nothing shows in the player list. The folder must be named exactly leaderstats, sit inside the Player and be made by a Script, not a LocalScript. Use IntValue, NumberValue or StringValue inside it.
  • Other players don't see a change. It was made in a LocalScript. Move it to a server Script.
  • Stats reset when you rejoin. Leaderstats never save by themselves. Add the save system from the DataStore tutorial linked above.
  • The board stays empty. Check the Output for warnings, then check that the place is published with Studio API access on. The board also skips scores of zero, so earn a win first.
  • A new win is missing from the board. Scores are written once a minute and when a player leaves, so wait for the next refresh.

Questions

Why is my leaderstats not showing in Roblox?

The folder must be named exactly leaderstats, in lowercase, and be parented to the Player object, not the character. Create it in a Script in ServerScriptService and put IntValue, NumberValue or StringValue objects inside it. If it still does not show, check the Output window for an error in the script.

How do you make a global leaderboard in Roblox?

Save each player's score to an OrderedDataStore, then read the highest scores with GetSortedAsync(false, 10) and draw them on a SurfaceGui in your lobby. Refresh it every 60 seconds or more. It only works in a published place, and Studio needs API access turned on for testing.

Can a LocalScript change leaderstats?

It can change the number on that player's own screen, but the change never reaches the server or anyone else. Change stats in server Scripts. When a GUI button is involved, fire a RemoteEvent and let the server check the request before it changes anything.

Does leaderstats save automatically?

No. The folder and its values disappear when the player leaves. To keep them, load the values from a DataStore when the player joins and save them when they leave, on a timer and when the server shuts down.

How often should a global leaderboard refresh?

Once every 60 seconds or slower is a good rule. Sorted reads have a smaller request budget than normal reads and writes, and players rarely notice that a board is a minute old.

Your game is
one sentence away.

Start building free

Free to start. No card needed.