A leaderboard displays information for players in a game like their scores, currency, or the fastest time in a race.
Setting Up the Leaderboard
The script in this tutorial will create a leaderboard that stores and displays a player’s gold amount, but it can be customized for different games.
Whenever a player enters the game, they’ll need to be added to the leaderboard along with code for tracking their individual stats.
- In the Explorer, under ServerScriptService, create a new script named PlayerSetup.
- In the script, add a function named
onPlayerJoin()with a parameter named
onPlayerJoin(), create a variable named
leaderstatsas a new
Folderinstance. All the stats for the player will be stored in this folder.
- Name the folder
leaderstatsand parent it to the player. If the folder is not named
leaderstats, Studio won’t know to create a leaderboard.
- After the closing
endof the function, connect
Players|PlayerAddedevent. Whenever a player joins the game, the
onPlayerJoin()function will run.
Don't Test Yet
If you test the project now, you won’t see a leaderboard. This is because the leaderboard doesn’t yet have any stats to display.
Showing Player Information
Leaderboards use values to store and display player statistics. This script will show a player’s gold using an
IntValue, a placeholder for a number.
leaderstats.Parent = player, create a new variable named
goldas a new IntValue instance.
Storing Values Other than IntValue »
A leaderboard can use
gold.Name = "Gold". This is the name that will appear in the leaderboard.
- On a new line, type
gold.Value = 0. This makes it so players don’t start with any gold.
While variables are normally changed using
= as in
myNumber = 10, an IntValue is changed using its Value property, like
myIntValue.Value = 10.
gold.Parent = leaderstats. This parents the IntValue for gold to
- Play your game and notice that a leaderboard appears in the top right.
Can't See Your IntValue? »
- Make sure that
- Make sure that the variable for the IntValue is parented to the leaderboard like
gold.Parent = leaderstats.
Leaderboards Don't Save Data
If a player leaves a game, they’ll lose any information in their leaderboard. To learn how to save data between player sessions, see
Articles/Saving Player Data|Saving Player Data.