Health Bar doesnt change when dying

why tho it works fine now? so whats the point?

It removes having to use a loop, instead using event-driven programming to present the data for the user. It also won’t bog up resources by having the events inside a while loop, causing a memory leak.

ok I see what you mean thx so much man

1 Like

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.