Creating a Leaderboard
Creating a Leaderboard
Players will be able to see important stats like how much gold they have, the number of items in their bag, and the max number of items they can hold by looking at a leaderboard.
Using Leaderboards In Different Ways »
Leaderboards won’t save information between games. If a player leaves a game, they’ll lose any information in their leaderboard.
Setting Up the Leaderboard Stats
Whenever a player is added to the game, they’ll need to be added to the leaderboard along with code for tracking the individual stats.
- In the Explorer, under ServerScriptService, create a new script named PlayerSetup.
- Delete Hello World and write a descriptive comment.
-- Creates a leaderboard that shows player variables
- After the comment, create a custom function named
onPlayerJoinwith a parameter named
local function onPlayerJoin(player)   end
onPlayerJoin, create a variable named
leaderstats, and have it create a new Folder Instance. Naming the folder
leaderstatslets Roblox Studio know to create a leaderboard.
local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") end
- Name the new Folder instance leaderstats, and parent it to the player.
local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player end
Make sure that you name the variable and the new instance leaderstats. Without this, Roblox won’t know to create a new leader board.
- After the end of the function, connect
PlayerAddedevent. Whenever a player joins the game, the
onPlayerJoinfunction will run.
leaderstats.Parent = player end game.Players.PlayerAdded:Connect(onPlayerJoin)
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.
Tracking Player Gold
Now that a leaderboard is created, it needs to show the player these numbers:
- Gold - How much money the player has.
- Items - How many items the player has collected from the world.
- Spaces - The most items a player can hold at one time.
Each of these numbers will be an IntValue, a placeholder object for a number.
- In OnPlayerJoin, under
leaderstats.Parent = player, type
local gold = Instance.new("IntValue").This creates a new IntValue and stores it in the variable
local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local gold = Instance.new("IntValue") end
gold.Name = "Gold". This gives the IntValue a name so you can use it in other scripts. The name will also be show up on the leaderboard.
local gold = Instance.new("IntValue") gold.Name = "Gold"
Using Your Own Values »
While this project uses the name “Gold”, students can use other forms of currency that better fit their theme like “Rubies” or “Gems”. To help students remember the exact spelling and names, have them write down the name in quotations on the Adventure Game Cheat Sheet handout.
- On a new line, type
gold.Value = 0. This makes it so players don’t start with any gold
local gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0
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
leaderstats. If the IntValue is not parented to
leaderstats, players won’t see it.
local gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0 gold.Parent = leaderstats
- 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.
Track Player Items and Spaces
- Add a blank line after
gold.Parent = leaderstats. This makes it easier to see where the code for different IntValues starts and stops.
- After the blank line, create a stat for Items by setting up a new IntValue the same way you did for gold.
gold.Parent = leaderstats   -- Create the Items stat local items= Instance.new("IntValue") items.Name = "Items" items.Value = 0 items.Parent = leaderstats
- Create a new stat for the player’s bag spaces. Set
spaces.Valueto 2 so players start the game only being able to hold two items at once, encouraging them buy a new bag as soon as they can.
items.Parent = leaderstats   -- Create the Spaces stat local spaces = Instance.new("IntValue") spaces.Name = "Spaces" spaces.Value = 2 spaces.Parent = leaderstats
- Test the game. Players should have a leaderboard showing Gold, Items, and Spaces.
Leaderboard Not Working As Expected?
- If you can’t see the number on the leaderboard, check that each IntValue is parented to leaderstats.
- Make sure each IntValue is spelled exactly as shown
- Check that the
PlayerAddedevent is at the bottom of the script
These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.