I want to make a script that deducts points on a leaderboard when a player dies in the game

I have already made a leaderboard script that randomly gives a player an amount of points, these points are the amount of lives the player has when he/she joins the game.

I don’t know how to deduct points from a leaderboard and I don’t know how to make the script deduct points (lives) from the player’s total when they die in the game.

(from falling off the map, sword fighting, etc)

When a player dies in the game I want the leaderboard to deduct one point (one life) from the counter and this continues every time the player dies until he/she has 0 lives.

I have no clue yet on how to start on putting these features in my script and I haven’t found this topic anywhere on Devforum.

Here’s my script that assigns a number to a player once they join and shows the number on the leaderboard

game.Players.PlayerAdded:Connect(function(players)
	local leaderboard = Instance.new("BoolValue", players)
	leaderboard.Name = "leaderstats"
	local Lives = Instance.new("IntValue", leaderboard)
	Lives.Name = "Lives"
	Lives.Value = math.random(10,100)
	print(Lives.Value)
	players.CharacterAdded:Connect(function(character)
	end)
end)

Can I make these features work in this script or do I have to make a separate script for it?
Do I need to make a death variable like my lives one?

**If someone could get me started or show me the full script that would make this feature possible that would solve my problem so I know how to make scripts like these again in the future.

Thanks for reading.

1 Like

Add a script in starter character scripts, it should be like this:

local char = script.Parent
local Human = char:WaitForChild("Humanoid")
local Players = game:GetService("Players")
local Player = Players:GetPlayerFromCharacter(char)

Human.Died:Connect(function()
    Player.leaderstats.Lives.Value -= 1
end)

This script will get the character and it’s humanoid (the thing that controls things like health and WalkSpeed for characters), and gets the player. We connect a function to a Humanoid.Died event, this function will fire once our character dies, in this function we just subtract one live from the player.

Edit: Added explanation

2 Likes

You can add a simple humanoid died event to subtract lives without having to create a new script like this:

game.Players.PlayerAdded:Connect(function(players)
	local leaderboard = Instance.new("BoolValue", players)
	leaderboard.Name = "leaderstats"
	local Lives = Instance.new("IntValue", leaderboard)
	Lives.Name = "Lives"
	Lives.Value = math.random(10,100)
	print(Lives.Value)
	players.CharacterAdded:Connect(function(character)
            local Humanoid = char:WaitForChild("Humanoid")
            Humanoid.Died:Connect(function()
                    Lives.Value -= 1
            end)
                     
	end)
end)

Thank you for responding, the script in character scripts now has made the feature possible. When I die in the game I lose one life on the leaderboard counter and it works now.