PcoWSkbVqDnWTu_dm2ix
Scoring Points
Part 3 - Resetting Points
Scoring Points
Part 3 - Resetting Points

Listening for Characters

The goal of the game is to see who can stay alive the longest, so players who die will need to have their points reset to 0.

You’ll need to get the Character model for the player in order to detect when they have died. This model is only added to the game after the Player object has been loaded and you can use the Player/CharacterAdded|CharacterAdded event to listen for when the character is ready to use.

  • Create a function called onCharacterAdded with two parameters: one for the character, one for the player.
local Players = game:GetService("Players")
	
local function onCharacterAdded(character, player)

end
	
local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")

Although you included player in the onCharacterAdded function’s parameters, the actual CharacterAdded event only returns the character, not the associated player.

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
	
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Handling Player Death

When a player dies, their Humanoid automatically fires a Humanoid/Died|Died event. You can use this event to find out when to reset their points.

The Humanoid is found inside the Character model, but the contents of that model are only assembled as the player spawns. To make your code safely wait for the Humanoid object to load, use the Instance/WaitForChild|WaitForChild function. You can call it on any parent object, passing the string name of the child you’re waiting for.

  • Create a variable to wait for the Humanoid using character:WaitForChild("Humanoid").
local Players = game:GetService("Players")

local function onCharacterAdded(character, player)
    local humanoid = character:WaitForChild("Humanoid")
end

The function you need to connect to the Died event is very short and will only ever be needed here, so you can use an anonymous function again.

  1. Connect a new anonymous function to the Humanoid’s Died event.

  2. In the anonymous function, create a variable called points for the player’s Points object.

  3. Set the Value property of points to 0.

    local Players = game:GetService("Players")
    
    local function onCharacterAdded(character, player)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
        	local points = player.leaderstats.Points
        	points.Value = 0
        end)
    end

Test this out and you’ll see the player’s score resets when they die.

Checking the Player

Even when dead, players keep earning points, which is hardly in the spirit of the game – the code needs to check if players are alive before awarding a point.

You’ll need to start by defining an attribute in the onPlayerAdded function which can be used to check whether the player is alive. At this point, the player is not yet alive and spawned, as their character model still needs to be added.

Attributes

Attributes allow you to customize objects in Roblox with your own data. An attribute consists of a name and a value. You can create one on any object using the Instance/SetAttribute|SetAttribute function.


  • Call SetAttribute on player to create a new attribute called “IsAlive” with the value false.
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

	player:SetAttribute("IsAlive", false)

	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Once the player’s character model respawns, the value of IsAlive needs to be changed to true so that the player can start earning points again.

  1. In onCharacterAdded, set the IsAlive attribute of player to true.

  2. In onCharacterDied, set the IsAlive attribute of player to false.

    local Players = game:GetService("Players")
    
    local function onCharacterAdded(character, player)
    	player:SetAttribute("IsAlive", true)
        local humanoid = character:WaitForChild("Humanoid")
    	humanoid.Died:Connect(function()
    		local points = player.leaderstats.Points
    		points.Value = 0
    		player:SetAttribute("IsAlive", false)
    	end)
    end

Finally, IsAlive should be checked before any point is awarded in the while loop at the end of the script. The Instance/GetAttribute|GetAttribute function takes the name of an attribute and returns the value.

  • In the while loop, wrap the code to award a point in an if statement with the condition player:GetAttribute("IsAlive").
while true do
	wait(1)
	local playerList = Players:GetPlayers()
	for currentPlayer = 1, #playerList  do
		local player = playerList[currentPlayer]
        if player:GetAttribute("IsAlive") then
             local points = player.leaderstats.Points
             points.Value = points.Value + 1
        end
	end
end

Test your game out now and you should find the player earns points every second they are alive, and stays at 0 when not alive. Have your friends play with you and see who can get the highest score.

This is just the start: you can continue improving your game and making it a better experience for your players. Here are things you can learn about in future courses.

  • How to place the code for all of the platforms into a single script, making it much easier to update.
  • Creating a lobby area where players wait to be teleported to the game area, allowing players to start simultaneously.
  • How to reduce lag in the game.
  • Announcing the winners of each round.
local Players = game:GetService("Players")

local function onCharacterAdded(character, player)
	player:SetAttribute("IsAlive", true)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		local points = player.leaderstats.Points
		points.Value = 0
		player:SetAttribute("IsAlive", false)
	end)
end

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

	player:SetAttribute("IsAlive", false)

	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)

while true do
	wait(1)
	local playerList = Players:GetPlayers()
	for i = 1, #playerList  do
		local player = playerList[i]
		if player:GetAttribute("IsAlive") then
			local points = player.leaderstats.Points
			points.Value = points.Value + 1
		end
	end
end


Previous Page Scripting the Points