The leaderboard system reads any values in the
leaderstats folder and displays whatever it finds.
To add a stat which will track a player’s points, a new IntValue object can be parented to the
leaderstats folder. The name of the value object will be displayed alongside its current value.
Use a variable named
pointsto create a new IntValue object using
Set the Name to
Set the Value to 0; this is what the leaderboard will initially display for the player.
pointsobject to the
local Players = game:GetService("Players") local function onPlayerAdded(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local points = Instance.new("IntValue") points.Name = "Points" points.Value = 0 points.Parent = leaderstats end Players.PlayerAdded:Connect(onPlayerAdded)
Test your game and you should see the leaderboard appear in the top right with the names of your players and a points score next to them.
Each player should earn a point for each second they are alive. A while loop and the
wait function can be used to update the value of points every second.
At the end of the script, create a while loop with
trueas the condition.
In the loop, wait for 1 second.
Players.PlayerAdded:Connect(onPlayerAdded) while true do wait(1) end
To run code for every player in the game, you’ll need to iterate through the array of players returned by the
An array is a list of items stored in order. Each item can be accessed by its index position, starting from 1. You can get the length of an array by prefixing it with #.
Store the result of
Create a for loop with a starting value of 1 and an end value of
#playerList, so that you get one iteration of the loop per player.
while true do wait(1) local playerList = Players:GetPlayers() for currentPlayer = 1, #playerList do end end
To award a point to each player in the for loop, you’ll need to get the player out of the array and add 1 to the Points object stored in their
Accessing Array Values
Objects stored in an array are accessed using square brackets - for instance, the first item in the
playerList array can be accessed with
If you write
playerList[currentPlayer] in the for loop, you can move through each player in the list with every iteration of the loop.
Store the player at
playerList[currentPlayer]in a variable called
Store the player’s Points object in a variable called
points.Value + 1.
while true do wait(1) local playerList = Players:GetPlayers() for currentPlayer = 1, #playerList do local player = playerList[currentPlayer] local points = player.leaderstats.Points points.Value = points.Value + 1 end end
Test your game and you should find that the leaderboard shows your player’s score counting up by 1 every second.
Previous Page Getting Started Next Page Resetting Points