PcoWSkbVqDnWTu_dm2ix
Scoring Points
Part 2 - Scripting the Points
Scoring Points
Part 2 - Scripting the Points

Points Value

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.

  1. Use a variable named points to create a new IntValue object using Instance.new.

  2. Set the Name to “Points”.

  3. Set the Value to 0; this is what the leaderboard will initially display for the player.

  4. Parent the points object to the leaderstats folder.

    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.

Counting Time

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.

  1. At the end of the script, create a while loop with true as the condition.

  2. In the loop, wait for 1 second.

    Players.PlayerAdded:Connect(onPlayerAdded)
    
    while true do
    	wait(1)
    end

Player List

To run code for every player in the game, you’ll need to iterate through the array of players returned by the Players/GetPlayers|GetPlayers function.

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 #.

  1. Store the result of Players:GetPlayers() in a playerList variable.

  2. 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

Awarding Points

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 leaderstats folder.

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 playerList[1].

If you write playerList[currentPlayer] in the for loop, you can move through each player in the list with every iteration of the loop.


  1. Store the player at playerList[currentPlayer] in a variable called player.

  2. Store the player’s Points object in a variable called points.

  3. Set the Value property of points to 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