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

In-Game Leaderboards

In-Game Leaderboards

Oct 05 2018, 4:36 PM PST 10 min

A leaderboard displays statistics for players currently in the game. It’s often used to show player scores, points, money, the fastest time in a race, and more.

Creating a Leaderboard

For a leaderboard to appear, every Player must contain a “leaderstats” object. This can be accomplished as follows:

  1. Add a new Script to ServerScriptService.
  2. Make the script insert an object named leaderstats into each player who enters the game. This object should be a Folder.
  3. Name the new object "leaderstats" and then parent it to the player.

Here’s an example function that inserts a Folder named "leaderstats" using the above workflow:

-- Function that executes when a player is added to the game
game.Players.PlayerAdded:Connect(
	function(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	end
)

Next, add a value instance like points or money to the leaderstats container:

  1. Create a new IntValue instance named money.
  2. Name the instance "Money" (this will be the name of the in-game leaderboard stat).
  3. Set the value of the instance to the starting value for each new player.
  4. Parent the instance to the leaderstats object.
-- Function that executes when a player is added to the game
game.Players.PlayerAdded:Connect(
	function(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local money = Instance.new("IntValue")
		money.Name = "Money"  -- Name of the in-game leaderboard stat
		money.Value = 0  -- Amount of money each new player starts with
		money.Parent = leaderstats
	end
)

That’s it! If you test the game now, the leaderboard should show up.

Changing Leaderboard Values

A player’s leaderboard stats can be changed by setting or adjusting the value of the named child within leaderstats. For example, this code cycles through all players in the game every 5 seconds and increases the value of Money by 10:

-- Function that executes when a player is added to the game
game.Players.PlayerAdded:Connect(
	function(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local money = Instance.new("IntValue")
		money.Name = "Money"  -- Name of the in-game leaderboard stat
		money.Value = 0  -- Amount of money each new player starts with
		money.Parent = leaderstats
	end
)

while wait(5) do 
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player:FindFirstChild("leaderstats") then
			local moneyStat = player.leaderstats.Money
			moneyStat.Value = moneyStat.Value + 10
		end
	end
end
Practice

Make a block part that adds 1 to a Touches leaderboard for every player who touches it.

Add this to a Script within ServerScriptService:

game.Players.PlayerAdded:Connect(
	function(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local touches = Instance.new("IntValue")
		touches.Name = "Touches"
		touches.Value = 0
		touches.Parent = leaderstats
	end
)

Next, add this to a Script which is a direct child of the block part:

script.Parent.Touched:Connect(
	function()
		for _, player in ipairs(game.Players:GetPlayers()) do
			if player:FindFirstChild("leaderstats") then
				local touchesStat = player.leaderstats.Touches
				touchesStat.Value = touchesStat.Value + 1
			end
		end
	end
)

Tags:
  • points
  • leaderboard
  • stats