Leaderboard

(Redirected from Leaderboards)

The leaderboard displays statistics for players currently in the game. They are often used to show player scores such as knockouts, wipeouts, or level.

Structure[edit]

For a leaderboard to appear, certain objects must be arranged in a specific way. The following example produces a leaderboard with stats KOs and Wipeouts:

  • Tree-collapse.png
    Players
    Players
    • Tree-collapse.png
      Player
      Player1
      • Tree-collapse.png
        Model
        leaderstats
        • IntValue
          KOs
        • IntValue
          Wipeouts
    • Tree-collapse.png
      Player
      Player2
      • Tree-collapse.png
        Model
        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:

New leaderboard.png


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[edit]

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:

  • Tree-collapse.png
    Players
    Players
    • Tree-collapse.png
      Player
      Player1
      • Tree-collapse.png
        Model
        leaderstats
        • IntValue
          KOs
        • IntValue
          Wipeouts
    • Tree-collapse.png
      Player
      Player2
      • Tree-collapse.png
        Model
        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[edit]

1) To make a leaderboard, start by inserting an object named 'leaderstats' into each player. This should be a Model or a Folder. If you use a *Value object, like an IntValue or StringValue, this may not work).

Select
game.Players.PlayerAdded:connect(function(player) --We create a function that will execute when PlayerAdded fires.
    local leaderstats = Instance.new("Model") --We create a Model
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player -- We parent the leaderstats object to the player
end) --closes function


2) Next, add the different values, such as the Money, KOs, 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.

Keep in mind that you can use other types of objects besides IntValue in leaderstats. The full list of working types is:

Other Value objects like ObjectValue are not supported.

Select
game.Players.PlayerAdded:connect(function(player)
     local leaderstats = Instance.new("Model")
     leaderstats.Name = "leaderstats"
     leaderstats.Parent = player
 
     local money = Instance.new("IntValue") --We create a new IntValue
     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)
     money.Parent = leaderstats -- Set the money object as a child of the leaderstats object.
 end)

Congratulations, if you test it now, the leaderboard should show up!

Extra[edit]

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

Select
game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 0
    money.Parent = leaderstats
end)
 
while wait(5) do 
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player:FindFirstChild("leaderstats") then
            player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1
        end
    end
end

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!

Exploiting[edit]

Unless Workspace.FilteringEnabled is set to true, an exploiter can change the value of a leaderstats object and the server will accept the changed value. For example, if you have a Knockouts IntValue, the exploiter could set the value to 100 in order to cheat. To prevent this, you should keep track of the value of each stat inside your server script, and the leaderstat values should just be used for display.

Select
--A table for holding all the player's leaderstat values
local playerLeaderstats = {} 
 
game.Players.PlayerAdded:connect(function(player)
    playerLeaderstats[player] = {}
    playerLeaderstats[player]["Money"] = 0
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = playerLeaderstats[player]["Money"]
    money.Parent = leaderstats
end)
 
while wait(5) do 
    for _, player in pairs(game.Players:GetPlayers()) do
        --We don't use the IntValue's value because an exploiter can modify it.
        playerLeaderstats[player]["Money"] = playerLeaderstats[player]["Money"] + 1
        if player:FindFirstChild("leaderstats") then
           player.leaderstats.Money.Value = playerLeaderstats[player]["Money"]
        end
    end
end

Practice[edit]

Exercise

Instructions

Make a block that adds 1 to the Taps leaderboard of every player when it is touched.

Solution
Select
--Add this as a script in the workspace
game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local taps = Instance.new("IntValue")
    taps.Name = "Taps"
    taps.Value = 0
    taps.Parent = leaderstats
end)
 
 
--Add this as a child of the part
script.Parent.Touched:connect(function()
	for _,Player in pairs(game.Players:GetPlayers()) do
	    if Player:FindFirstChild("leaderstats") then
	        Player.leaderstats.Taps.Value = Player.leaderstats.Taps.Value +1
	    end
	end
end)