Health Bar doesnt change when dying

under ? like this

end)
end

and the lines should be down there?

Like this:

local Health = script.Parent.Health
local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(Char)
    local Humanoid = Char:WaitForChild("Humanoid")
    local HCConn, DConn

    HCConn = Humanoid.HealthChanged:Connect(function(CurrentHealth)
        Health.Text = tostring(math.floor(CurrentHealth))
    end)

    Health.Text = tostring(math.floor(Humanoid.MaxHealth))

    DConn = Humanoid.Died:Connect(function()
        HCConn:Disconnect()
        DConn:Disconnect()
        Health.Text = "0"
    end)
end)

EDIT
Try it now lol.

EDIT2
Ohhhhhhhhhh I didn’t see you had ResetOnSpawn disabled. Rewrote the code

now its just 0 and it never went back to 100

You want to make the script in a loop because when the character dies the character and humanoid variables wont be up to date.

its still 0 are you sure everythings correct?

Should be now lol, I didn’t notice you had ResetOnSpawn disabled. Rewrote the above code to include that.
EDIT

Nope, exactly how it’s written.

shouldnt itLocalPlayer before doing CharacterAdded?

oh wait nope nvm i see it now hmmm

That script wont work because it needs to be a server script.
This should work:

local Health = script.Parent.Health
local player = game.Players.LocalPlayer

while task.wait() do
   local Char = player.Character or plr.CharacterAdded:wait()
   local Hum = Char:WaitForChild("Humanoid")
   Health.Text = ""..math.floor(Hum.Health)
end

well his script still works for some reason StormDragon23

That’s incorrect – the code you wrote will not work. LocalPlayer is client-sided only and is nil on the server. Regardless, this does work on a LocalScript, including the version of the program I presented.

2 Likes

ok it wont work cuz the humanoid will be removed once the character dies and will give a error ofc

you can either use a protected call inside the HealthChanged event or make a function will be called when the character added after the death

local Health = script.Parent.Health
local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()

local function OnCharacterAdded(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
	Health.Text = tostring(math.floor(Humanoid.Health)) -- setting it for the first time
	local HCConn = Humanoid.HealthChanged:Connect(function(CurrentHealth)
		Health.Text = tostring(math.floor(Humanoid.Health))
	end)
	Humanoid.Died:Wait()
	HCConn:Disconnect()
end

OnCharacterAdded(Char) -- calling it for the first time 

player.CharacterAdded:Connect(OnCharacterAdded) 
1 Like

so which solution do i use CookieInks or StormDragon23 one?

1 Like

You’re correct and on point, but I’ll expand upon this – the problem with the original program was that it was still holding a reference to the original Humanoid, even after the player had respawned and got a new character. Thus, even though the player got a new character with a new sparkly Humanoid, the program was still referencing the original. The solution’s to use CharacterAdded to get the new character and grab the new Humanoid.

EDIT

Thanks for selecting my solution!

1 Like

yours made more sense and you were the first one to do it

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Frame = script.Parent
local Health = Frame:WaitForChild("Health")

local function OnCharacterAdded(Character)
	local function OnHealthChanged(Health)
		Health.Text = math.round(Health)
	end
	
	local Humanoid = Character:WaitForChild("Humanoid")
	Health.Text = Humanoid.Health
	Humanoid.HealthChanged:Connect(OnHealthChanged)
end

Player.CharacterAdded:Connect(OnCharacterAdded)

You don’t need tostring() and you don’t need to disconnect the created connections, they are automatically disconnected when the humanoid instance is destroyed.

We should also add a WaitForChild() call on the GuiObject named “Health” to allow for it to replicate to the client.

so? should i use yours or could i just use his which one would be better?

It doesn’t really matter which you use, I was just providing a few pointers.

Interesting that tostring isn’t required. I’m iffy about that second point however since you can bring the old character “back to life” (meaning it isn’t Destroyed, thus you can bring the original character back. This is why the following code’s possible).

Player.CharacterRemoving:Connect(function(OldCharacter)
    OldCharacter.Parent = game.Workspace
end)

When a player’s character’s removed it is not Destroyed. To make sure the character’s destroyed, you’d have to use the above event (CharacterRemoving), pass the old character to the function and Destroy it.

Player.CharacterRemoving:Connect(function(OldCharacter)
    OldCharacter:Destroy()
end)

Note that this code has to used in a (server) script and not a (client) localscript to have its being Destroyed replicated to all clients, the server included. Otherwise only using it in the latter will only destroy it on that client and not the server nor other players.

–

EDIT

I never said it did. As I explained prior, it fires when the old player’s character’s removed and passes it to a function. I can kinda see where you might have gotten that impression, but it’s a stretch imo. Added a few points for clarification.