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

Setting up a Leaderboard

Setting up a Leaderboard

Prerequisites
  • General knowledge of Studio covered in Intro to Studio
  • Knowledge of scripting covered in Coding 2
  • Students should have completed Introducing the Adventure Game
  • Students should have a basic leaderboard
  • Lesson Time 10 - 15 minutes
    Optional Handouts
  • Adventure Game Reference
  • Learning Objectives
  • Understand how to display player variables like gold using a leaderboard
  • Learn how to use IntValues to store player variables
  • Now that you’ve created a leaderboard, 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 one of these numbers will be an IntValue and follow the same series of steps when creating the Gold number.

    If you haven’t created a leaderboard yet, you can use the code in the box below.

    -- In 'ServerScriptService', add this into a script named 'PlayerSetup'
    local function onPlayerJoin(player)
    	local leaderstats = Instance.new("Folder")
    	leaderstats.Name = "leaderstats"
    	leaderstats.Parent = player
    
    	-- Display an 'IntValue' on leaderboard
    	local gold = Instance.new("IntValue")
    	gold.Name = "Gold"
    	gold.Value = 0
    	gold.Parent = leaderstats
    end
    
    -- Run 'onPlayerJoin()' when the 'PlayerAdded' event fires
    game.Players.PlayerAdded:Connect(onPlayerJoin)

    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.