How would I add a "Lives" system?

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 also wish to find out how to make it so when the number of lives reaches 0, the player gets teleported back to the start or the previous checkpoint (every 10 levels?)

My game already has a system where if you die in a level you repeat that level, so I was thinking that if your lives drain completely then you would be taken back to the last multiple of 10 level, example: 0, 10, 20, 30, etc.

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.

3 Likes

Check if a player dies when they die subtract their leaderstatds put an if statment their so if their lives == 1 and they die it becomes 0 so they teleport back

You could use the Humanoid.Died listener to check when the player dies and then subtract 1 from the number of lives the player has left. If the number of lives left is 0, teleport the player back to the previous checkpoint

Example:

local lives = 5
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
    v.CharacterAdded:Connect(function(character)
        if character ~= nil then
            character.Humanoid.Died:Connect(function())
                if lives > 0 then
                    lives = lives - 1
                else
                    --teleport player back to checkpoint
                    lives = 5
                end
            end
        end
    end
end
6 Likes

I already have it so when you start playing your lives are at 5, but when you are at 0 lives and sent to the checkpoint, how do I make it so that the lives go back to 5?

Whoops! I accidentally submitted my reply too early.

As others have said, you may utilise the Humanoid.Died event to check for a player’s death, then perform actions based on that.

You may use this little code snippet, with character being the character retrieved from an event such as player.CharacterAdded.

		character:WaitForChild("Humanoid").Died:Connect(function()
		-- insert code here

As for teleporting people back to a location after their lives hit 0, you should maybe try using a pad that sets a value in the player, then when a player’s lives do hit 0, it gets the position of the pad which they last stepped on and just teleports them to that.

All above examples work, although you should try what works for you in terms of what scripts you have.

3 Likes

You can set the player’s lives back to 5 after you teleport the player back to the checkpoint

if lives > 0 then
    lives = lives - 1
else
    --teleport player back to checkpoint
    lives = 5
end

OK, so I tried the thing you told me, and it didn’t work. The lives remained the same as I died, and I did link it to the leader stat.

I changed up my previous post to use the Player.CharacterAdded listener. I think the original code only listened for the player’s death only once, which might be why the number of lives didn’t change. Maybe the new code should work?

It still didn’t work. Am I putting it in the wrong place? I have it in serverscriptservice

Are there any errors in your code?

I fixed some of the syntax errors in my previous code.
This should be in a server script placed in serverscriptservice

local lives = 5
game.Players.PlayerAdded:Connect(function(p)
    p.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            if lives > 0 then
                lives = lives - 1
                --update leaderstats value
            else
                --teleport player back to checkpoint
                lives = 5
                --update leaderstats value
            end
        end)
    end)
end)
2 Likes

Hello! is this still available? (what I mean is do you still need help.)