Unable to detect a change in Humanoid.Health

Hello developers,

I’m trying to detect a change in the value of Humanoid.Health, but for some reason it only works when the character dies (not after it’s died, only when it dies). I’ve tried Humanoid:GetPropertyChangedSignal("Health"), Humanoid.HealthChanged and Humanoid.Changed

Heres my code (this is running on the server):

local function setupUI(plr: Player)
		local plrUI = ui.CrewMember:Clone()
		plrUI.Parent = frame
		plrUI.Name = plr.Name
		plrUI:WaitForChild("CrewName").Text = plr.Name
		
		local healthConnection
		local charConnection
		local char = plr.Character or plr.CharacterAdded:Wait()
		local hum: Humanoid = char:WaitForChild("Humanoid")
		
		local function onHealthChanged()
			print("hc")
			plrUI.Health.Bar.Size = UDim2.fromScale(hum.Health / 100,1)
		end
		
		healthConnection = hum:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
		
		charConnection = plr.CharacterAdded:Connect(function(char)
			print("char added")
			
			healthConnection:Disconnect()
			
			hum = char:WaitForChild("Humanoid")
			
			plrUI.Health.Bar.Size = UDim2.fromScale(1,1)
			
			healthConnection = hum:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
			
			game.Players.PlayerRemoving:Connect(function(player)
				if player == plr then
					plrUI:Destroy()
					diedConnection:Disconnect()
				end
			end)
		end)
		
	end

You do not need to set the text of a player’s GUI from the server! You don’t have to bother manually removing the GUI either! Put this as a LocalScript under ui.CrewMember:

local plrUI = script.Parent -- put this script under `ui.CrewMember`, or make a copy and parent locally
plrUI:WaitForChild("CrewName").Text = game.Players.LocalPlayer.Name

local charAdded = function(character)
	local humanoid = character:WaitForChild("Humanoid")

	local function onHealthChanged()
		plrUI.Health.Bar.Size = UDim2.fromScale(humanoid.Health / 100, 1)
	end
	humanoid:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
end

game.Players.LocalPlayer.CharacterAdded:Connect(charAdded)
if game.Players.LocalPlayer.Character then charAdded(game.Players.LocalPlayer.Character) end

Also helped you clean up the code a bit :slight_smile:

sorry, i forgot to mention that the ui is a surface gui.

Ohh! In that case, are you trying to display the local player’s health on that surface gui?

Just confirming that the script is not located inside the workspace, because LocalScripts do not run in workspace.

the surface gui contains every players health

This code runs on the server, so it’s stored in ServerScriptService

Try this (Server script)
Presumably setupUI should be called in PlayerAdded

local ui = {}
local function setupUI(plr: Player)
	local plrUI = ui.CrewMember:Clone()
	plrUI.Parent = frame
	plrUI.Name = plr.Name
	plrUI:WaitForChild("CrewName").Text = plr.Name
	if ui[plr] then ui[plr]:Destroy() end
	ui[plr] = plrUI
	local charAdded = function(character)
		local humanoid = character:WaitForChild("Humanoid")
	
		local function onHealthChanged()
			plrUI.Health.Bar.Size = UDim2.fromScale(humanoid.Health / 100, 1)
		end
		humanoid:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
	end

	plr.CharacterAdded:Connect(charAdded)
	if plr.Character then charAdded(plr.Character) end

end

game.Players.PlayerRemoving:Connect(function(plr)
	if ui[plr] then ui[plr]:Destroy() end
	ui[plr] = nil
end

This isn’t tested, let me if it works or not!

Also, what is diedConnection?

the code i’m currently using works fine, the only thing that doesn’t work is humanoid:GetPropertyChangedSignal("Health"):Connect(onHealthChanged).

try running this code in your command bar:

local humanoid = game.Players.[your username here].Character.Humanoid

humanoid.HealthChanged:Connect(function()
    print("E")
end

when you take damage it should print e. for me it only prints e when i die.

It’s better if you clean up your code, since that PlayerRemoving connection can most likely cause a memory leak!

Also, the code seems to be working for me! Are you changing the health locally or on the server? The Health property of the Humanoid doesn’t replicate to the server, but only does when the player dies!

2 Likes

thanks! I didn’t know that health didn’t replicate to the server.

Thank you so much dude! You are incredible !
Im going to go test it now! Thanks again man !
This is gonna be really helpful!
okay bye now !
see you later!
!!!