Change the visibility of a label image when local player health reaches 0

Hello, I’m currently trying to make it so when the player dies and their Humanoids health reaches 0, a label image’s visibility is set to false making the image no longer to be seen, until their HP has gone back to 100.

Here is what I’ve tried so far…

I was just wondering if I could get some help, much appreciated.

I believe your problem is that you are comparing Humanoid.Health (a number) to a string. When you should just be comparing it to a number.

if Humanoid.Health <= 0 then

Additionally, you should be using a event to detect when a humanoid dies.

Or if you are just trying to find out if their health changes you can easily use this event

You have to use the HealthChanged event that comews from the humanoid. So you’d have to do something like

Humanoid.HealthChanged:Connect(function(health)
    if health <= 0 then
         --Code
    else
         --Code
    end
end)

Also your code to cahnge the visible wont work as you need PlayerGui, not StarterGui. If the localscript is in Startergui, then you go back through the hierachy till you reach StarterGui, or you could use `game.Players.LocalPlayer:WaitForChild(“PlayerGui”)

A couple things:

  • You should use WaitForChild() cause sometimes the Character doesn’t exactly load in time with the script

  • There is actually a way to check for a Player’s Health changing, and that’s the Humanoid.HealthChanged event

  • Humanoid.Health is a Float (Value), not a StringValue lol

“Health_Images” and “Heart_full” will result in errors, change those to ["Health_Images"] and ["Heart_full"] (I’m used to assuming every special symbol will result in an error, so mb ignore that one)

  • Also, you’re getting the StarterGui, not the PlayerGui :pensive:

Try this and see if it fixes?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Gui = Player:WaitForChild("PlayerGui")

Humanoid.HealthChanged:Connect(function()
    if Humanoid.Health <= 0 then
        Gui.Health["Health_Images"]["Heart_full"].Visible = false
elseif Humanoid.Health >= 1 then
        Gui.Health["Health_Images"]["Heart_full"].Visible = true
    end
end)

Health_Images and Heart_full are both valid names. You don’t have to place them inside brackets.

1 Like

You can use Humanoid.Died to detect if a player’s humanoid dies

1 Like

sorry for the late response. Yeah, unfortunately that didn’t work for me. Though I was able to use the information you and everyone else gave me to produce this. wwwwwwwwwwwwwwwwwww

There isn’t any issue in the console anymore, though for some reason this still isn’t working lol, sorry for bothering, but I’m new to scripting.

You’re only getting the currentHealth variable (Which is not being changed every time the health changes), not the actual health parameter the HealthChanged Event gives you, use that instead

1 Like

uhhh

Just put this:

humanoid.HealthChanged:Connect(function(ActualHealth)
    if ActualHealth <= 0 then
        Gui.Health.Full.Visible = false
    elseif ActualHealth >= 1 then
        Gui.Health.Full.Visible = true
    end
end)

Also, don’t put else if separately, you just need elseif on its own