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.
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.
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.
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.
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 is a variable accessible from anywhere which contains everything in your game.
Create a variable for the Players service using
Create a function called
onPlayerAddedwith a parameter for the incoming player.
onPlayerAddedfunction 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
Create a new Folder object using
Instance.new("Folder"),storing the result in a new variable called
local function onPlayerAdded(player) local leaderstats = Instance.new("Folder") end
Set the Name property of
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)
Make sure you name the folder exactly as it is shown here (
"leaderstats") or it will not work!
Next Page Scripting the Points