Leaderboards

From Legacy Roblox Wiki
(Redirected from Leaderboard)
Jump to navigationJump to search

In Roblox, a leaderboard is a display of various stats of players currently in the game.

Structure

For a leaderboard to appear, certain Objects must be constructed in a certain way. Note that for the individual stats, you must use IntValues. The following example produces a leaderboard with stats KOs and Wipeouts:

  • Players
    Players
    • Player
      Player1
      • IntValue
        leaderstats
        • IntValue
          KOs
        • IntValue
          Wipeouts
    • Player
      Player2
      • IntValue
        leaderstats
        • IntValue
          KOs
        • IntValue
          Wipeouts

Let's say that each Player represents a row of a table. Then, by itself, the "leaderstats" object represents a container for each column of the table, and each IntValue in that represents a column, like so:

leaderstats     :    Container
    KOs         :    Column
    Wipeouts    :    Column

This produces a table with players for each row, and stats for each column:

KOs Wipeouts
Player1
Player2

In-game it will look like:


To fill in each cell of the table, the value of each stat in each player is used. Let's revisit the player structure, displaying the values of each IntValue:

Player1
    leaderstats  
        KOs         :    14
        Wipeouts    :    31
Player2
    leaderstats
        KOs         :    29
        Wipeouts    :    8

With that, the table would look like this:

KOs Wipeouts
Player1 14 31
Player2 29 8



Consistency

For a leaderboard to appear, every Player must contain a leaderstats, and each of these leaderstats must contain objects with the same type and the same name. This is so the leaderstats table can be properly displayed.

Say you had a structure with inconsistent stats:

  • Players
    Players
    • Player
      Player1
      • IntValue
        leaderstats
        • IntValue
          KOs
        • IntValue
          Wipeouts
    • Player
      Player2
      • IntValue
        leaderstats
        • IntValue
          KOs
        • IntValue
          Points

Each column wold be incomplete: the Wipeouts column doesn't have a value for Player2, and the Points column doesn't have a value for Player1. This would mean that both wipeouts and points would not display, only KOs.

Creation

1) To make a leader-board, you will have to start by inserting an object named 'leaderstats' into each player:

game.Players.PlayerAdded:connect(function(player) --We create a function that will execute when PlayerAdded fires.
    local leaderstats = Instance.new("Model", player) --We insert an Model into the the new player (player)
    leaderstats.Name = "leaderstats"
end) --closes function

2) Now, we will add the different values, such as the Money, KO's, and Wipeouts, to the leaderstats container. In this tutorial, we will only have a Money section. To do this, all you have to do is add an 'IntValue' object as a child of the 'leaderstats' container.

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstats) --We instance a new IntValue as a child of 'leaderstats'
    money.Name = "Money" --this is the name you want the leader-stat to be when it shows up in-game.
    money.Value = 0 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
end)

Congratulations, if you test it now, the leader-board should show up!

Extra

Here is just some extra code that makes the money value go up every 5 seconds.

local playerStats = {} --this keeps a list of the stats for each player that enters the game

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"
    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Money"
    money.Value = 0

    stats[player] = leaderstats 
end)

while true do --infinite loop
    for player, stats in ipairs(playerStats) do --loops through each player => stat pair in the table
        stats.Money.Value = stats.Money.Value + 1
    end
    wait(5)
end

A more advanced scripter could make a system like this:

local stats = {"Eggs", "Chickens", "Pies"}
game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"
    for _, stat in ipairs(stats) do
        Instance.new("IntValue", leaderstats).Name = stat 
    end
end

This allows you to add many more leaderstats without lots of lines of code.

Also, remember if you don't want to display everything, just leave them outside of "leaderstats" and they won't show up, but they won't reset when the player dies if you put them directly in the Player- This can be usefull!