How to lose a life when you die

I been working on a 2-D platformer game, and am planning to add in a system for lives.

I already made it a leaderboard stat, in which you start with 5 lives, but I am not particularly sure how to make it so that it loses a live whenever the player dies.

I was just wondering how I could get this implemented, as I have figured out how to make hearts to collect that add to the leaderboard value.

(I asked a similar question before but didn’t get any helpful responses.

2 Likes

Use humanoid.Died

player.Character.Humanoid.Died:Connect(function()
    player.leaderstats.Lives.Value = player.leaderstats.Lives.Value - 1
end)

that is to subtract their lives

4 Likes

Where would I put the script? I tried server script storage, and it wasn’t working.

1 Like

Sorry, I meant server script service**

1 Like

The player is supposed to be referenced in a local script which is most commonly put inside StarterGui, but for this case it might be more suitable to put it inside of StarterPlayerScripts.

local player = game.Players.LocalPlayer

So change that.

2 Likes

I tried it, and the lives still remain the same.

May you show us the script you are currently using?

1 Like

local player = game.Players.LocalPlayer
player.Character.Humanoid.Died:Connect(function()
player.leaderstats.Lives.Value = player.leaderstats.Lives.Value - 1
end)

You’re changing your leaderboard stat on the client. This means it will not replicate to the server. Are you aware? Or was this the intended behavior?

3 Likes

easy modification FireServer and server does it

1 Like

The game is a single player game, and it won’t be multiplayer. As it is a 2D platformer.

The issue might be because you’re not connecting the Died signal to the player’s new character’s Humanoid. When a player’s character is added, await for their new humanoid to be added, then when they die remove a life.

1 Like
game.Players.PlayerAdded:connect(function(plr) 
plr.CharacterAdded:connect(function()
local hum = plr.Character:WaitForChild("Humanoid")
     hum.Died:connect(function()
     local leaderstats = plr:FindFirstChild("leaderstats")
     local stat = leaderstats:FindFirstChild("lives") -- you might have to change that
     stat.Value = stat.Value -1
 end)
 end)
 end)

Try something similar to this?

1 Like

Where would I put this? ServerScriptService? Also would it be local or normal (as in script type)

I’m sorry, i’m not the best scripter. lol

local plr = game.Players.LocalPlayer

plr.CharacterAdded:Connect(function(CharacterAdded)
    plr.Character.Humanoid.Died:Connect(function(Died)
    plr.leaderstats.Lives.Value = player.leaderstats.Lives.Value - 1
    end
end)

Local Script.

Put the script in the StarterPlayerScripts.

1 Like

A normal script inside the server script service. This script might not be the best example though, I’m not that great of a scripter.

1 Like

:connect is deprecated. You should use it in favor of :Connect and you should indent your code properly.

Also you might want to make sure the character already exists and to call your CharacterAdded function on the already existing character if that is the case.

Also @Deezylolz you would put this in StarterPlayerScripts or ReplicatedFirst likely. Since your game is 1 player, and if you’re not worried about players cheating, then you can just do everything on the client. However if you don’t want player’s cheating then I suggest using some Scripts on the server.

1 Like

Didn’t realize I made that many mistakes, sorry.

1 Like

Yep… that didn’t work either.

I’m not too sure what to do now

I just gave you an code, use it!

1 Like