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.
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.
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.
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)
: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.