Humanoid doesn't update, even if CharacterAdded is in place

I’m attempting to create a textlabel that’ll display the player’s health and changes in every HealthChanged event, but it doesn’t seem to work after the character has respawned. What’s the issue here? This is in a localscript.

local char = plr.Character or plr.CharacterAdded:Wait()
	local health = char.Humanoid.Health
	local colour
	if health >= 75 then
		colour = "(129, 218, 115)"
	elseif health >= 50 then
		colour = "(182, 218, 115)"	
	elseif health >= 25 then
		colour = "(218, 150, 105)"
	elseif health >= 0 or health <= 0 then
		colour = "(218, 84, 84)"	
	perkBg.EH.Health.Text = '- you currently have <font color= "rgb'..colour..'">'..char.Humanoid.Health..'</font> health'

I think you need to use server script with CharacterAdded event so it could update it even after character is respawned since it attach HealthChanged event to character again.

After that you can use FireClient to tell client to change it’s text label to the color corresponding to your IF statements

If you are not familiar with client/server events and functions you can use this article

Hope this helped :slight_smile:

@Spelo1 CharacterAdded always works in the server and local script. I did many CharacterAdded in a local script and it nevers fails me.

@OP, have you try to do print to see if HealthChanged fired? It’s better to print debug before you create a topic.

Yes, I debugged it, but the humanoid isn’t updated and it doesn’t print after respawned.
edit - meant to reply to @Quwanterz

What is the parent of the local script?

A frame under ScreenGui, that’s in StarterGui.

It seems like this isn’t the full script, mind sending the other variables like perkBg?

It’s a 200 lines of coding, but:

local frame = script.Parent
local purchasePerk = frame.PurchasePerk
local perkBg = purchasePerk.Background

I’ll try to replicate your script and see if it works for me.

I also attempted your method of RemoteEvents, but instead, it doesn’t change the value at all - or just doesn’t change when CharacterAdded has respawned.

Did a quick test, put local script into frame under ScreenGui under StarterGui. Used command bar to take damage. It printed changing of health even after respawn. Seems like problem in his code of some sort. Same with changing text on text label btw, it changes even after respawn

I’ve had this issue in the past if the LocalScript was placed in StarterPlayerScripts, or if the code was present in a module script, where respawning causes the script to lose reference to the Player’s Character. This is because a new Character model would be loaded, and the old one would be destroyed. This results in the reference to the Character in the script being outdated.

That being said, if your ScreenGui doesn’t have ‘ResetOnSpawn’ disabled, this script should re-load every time you respawn with the GUI and get the new Character reference properly. For example, the following snippet of code placed in the same location works for me:

local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()

print("Get the character!")


  print("Health changed!")


Now this being said, if you don’t want this GUI to reset when the player respawns for some reason and have “ResetOnSpawn” disabled, you will need to either call a function to detect when the player respawns, and get their new Character model or put this script in StarterCharacterScripts and have it reference the GUI externally. Both methods should guarantee that you’d be getting the new spawned-in Character after the player resets.