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()
char:WaitForChild("Humanoid").HealthChanged:Connect(function()
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)"
end
perkBg.EH.Health.Text = '- you currently have <font color= "rgb'..colour..'">'..char.Humanoid.Health..'</font> health'
end)
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
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!")
char:WaitForChild("Humanoid").HealthChanged:Connect(function()
print("Health changed!")
end)
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.