PcoWSkbVqDnWTu_dm2ix
Scoring Points
Part 1 - Getting Started
Scoring Points
Part 1 - Getting Started

In previous courses, you made a variety of game features including fading platforms and deadly lava. This course ties these together into a playable game where players see who can stay alive the longest. Every second they stay alive, a point will be added to their score.

Setting Up

First up, you’ll need to set the scene for your game. Duplicate the fading platforms you made in the previous course and make players compete to stay on the board of platforms for as long as possible.

You can also use deadly lava to kill players when they fall off the platforms, or just let them fall to their doom. Make sure you place a SpawnLocation somewhere where players can jump onto the platforms to start playing.

Spawn point above a grid of fading platforms, above a lava floor

Player Points

Roblox has a built-in Leaderboard for showing player stats. When you set a player’s points through the leaderboard, it’ll show up on the right side of the screen in the game.

Leaderboard with player name and points shown

You’ll learn more customizable ways to display information in later courses, but the leaderboard is the simplest way of making a visible scoring system in Roblox.

It’s best to put scripts which set up game state into ServerScriptService. Scripts placed there will automatically run when the game starts.

  • In ServerScriptService, create a script called SetupPoints.

    SetupPoints script in place in ServerScriptService in the Explorer window

Listening for Players

In Roblox, a service is an object which performs a range of useful functions. The Players service has an event called Players/PlayerAdded|PlayerAdded that you can use to set up points for each player who joins the game.

You can access services with the GetService function in the game object. game is a variable accessible from anywhere which contains everything in your game.

  1. Create a variable for the Players service using game:GetService("Players").

  2. Create a function called onPlayerAdded with a parameter for the incoming player.

  3. Connect the onPlayerAdded function to the PlayerAdded event.

    local Players = game:GetService("Players")
    
    local function onPlayerAdded(player)
    	
    end
    	
    Players.PlayerAdded:Connect(onPlayerAdded)
Variables for Services

When declaring a variable to contain a service, it’s best to name it with the exact name of the service (Players), even though this means breaking usual naming conventions for variables.


Create a Stats Folder

To make a player’s points display in the leaderboard, all you need to do is create a new Folder in their Player object called "leaderstats" and put their points in there. New objects can be created from within a script via the Instance.new function.

  1. Create a new Folder object using Instance.new("Folder"), storing the result in a new variable called leaderstats.

    local function onPlayerAdded(player)
    	local leaderstats = Instance.new("Folder")
    end
  2. Set the Name property of leaderstats to "leaderstats".

  3. Parent leaderstats to player.

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

Next Page Scripting the Points