UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

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.

  1. In the Explorer, under ServerScriptService, create a new script named PlayerSetup.
  1. Delete Hello World and write a descriptive comment.
-- Creates a leaderboard that shows player variables
  1. After the comment, create a custom function named onPlayerJoin with a parameter named player.
local function onPlayerJoin(player)
 
end
  1. In onPlayerJoin, create a variable named leaderstats, and have it create a new Folder Instance. Naming the folder leaderstats lets Roblox Studio know to create a leaderboard.
local function onPlayerJoin(player)
    local leaderstats = Instance.new("Folder")

end
  1. 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
Troubleshooting Leaderboards

Make sure that you name the variable and the new instance leaderstats. Without this, Roblox won’t know to create a new leader board.


  1. After the end of the function, connect OnPlayerJoin to the PlayerAdded event. Whenever a player joins the game, the onPlayerJoin function 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.

  1. In OnPlayerJoin, under leaderstats.Parent = player, type local gold = Instance.new("IntValue").This creates a new IntValue and stores it in the variable gold.
local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue")

end
  1. Type 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"

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.


  1. 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
Using .Value

While variables are normally changed using = as in myNumber = 10, an IntValue is changed using its Value property, like myIntValue.Value = 10.


  1. Type 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
  1. Play your game and notice that a leaderboard appears in the top right.
  • Make sure that .Value is capitalized.
  • Make sure that the variable for the IntValue is parented to the leaderboard like gold.Parent = leaderstats.

Track Player Items and Spaces

  1. Add a blank line after gold.Parent = leaderstats. This makes it easier to see where the code for different IntValues starts and stops.
  2. 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
  1. Create a new stat for the player’s bag spaces. Set spaces.Value to 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
  1. 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 PlayerAdded event is at the bottom of the script

-- Creates a leaderboard that shows player variables
local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats

	local items= Instance.new("IntValue")
	items.Name = "Items"
	items.Value = 0
	items.Parent = leaderstats

	local spaces = Instance.new("IntValue")
	spaces.Name = "spaces"
	spaces.Value = 2
	spaces.Parent = leaderstats

end

-- Run onPlayerJoin when the PlayerAdded event fires
game.Players.PlayerAdded:Connect(onPlayerJoin)


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.