I'm attempting to make a GUI that displays the amount of lives the player has. Each time they die they are supposed to lose a life. I placed into the Workspace a local script with the following code.
1 | local player = game.Players.LocalPlayer |
2 | G_lives = 3 ; |
3 |
4 | if (player.health = = 0 ) then |
5 | G_lives = G_lives - 1 |
6 | print (G_lives) |
7 | end |
I'm getting no output from this though, and I'm not exactly sure why. Could anyone explain the flaw in my code?
Player
object. You get it from the Humanoid
.1 | local plr = game:GetService( "Players" ).LocalPlayer |
2 | local char = plr.Character or plr.CharacterAdded:Wait() |
3 |
4 | char.Humanoid.HealthChanged:Connect( function (newHealth) |
5 | print ( "The new health of the humanoid is:" , newHealth) |
6 | end ) |
The health isnt in the player, its in the Character or the Character's humanoid. This means you have to go player.Character.Humanoid.Health or player.Character.Health
I changed my code to this:
1 | local player = game:GetService( "Players" ).LocalPlayer |
2 | local char = player.Character or player.CharacterAdded:Wait() |
3 |
4 | if (char.Humanoid.Health = = 0 ) then |
5 | G_lives = G_lives - 1 |
6 | print (G_lives) |
7 | end |
I'm still not getting any output though. Did I misuse the information you guys gave me, or is it possibly because I have the localscript under workspace and it should be somewhere else?