Multiplayer, saving and debugging, lesson 29
leaderstats
Goal: Show a stat in the player list.
about 6 min3 practice exercises
Roblox has a built-in leaderboard: the player list in the corner. To show a stat there, give each player a Folder named leaderstats containing value objects.
-- Script in ServerScriptService
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats
end)
The details that matter:
- The folder's name must be exactly
leaderstats, all lowercase. Roblox looks for that name. - It is parented to the player, not the character, so it survives respawns.
IntValueholds a whole number. The stat'sNameis the column heading.
To change a player's coins, set the value from a server script:
player.leaderstats.Coins.Value += 5
This is where the last three lessons meet. The stat lives on the server, the server changes it, and Roblox shows it to everyone. A LocalScript that changes it only fools its own screen.
For now the coins are lost when the player leaves. The next lesson fixes that.
Try it in Studio
Optional, and the best way to make it stick.
Add the lesson's script to ServerScriptService and press Play. Your name appears in the player list with a Coins column.
Go deeper on Rodex
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.