Health Changed Doesnt Work

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")	
		Humanoid.HealthChanged:connect(function(Health)
			print("changed")
		end)
	end)
end)
	

How come this code doesnt work (server script) when you changed the humanoids health?

1 Like

The .HealthChanged event won’t fire if you’re manually changing the health value with the properties window, otherwise your script appears to be working fine.

This can be demonstrated by the following:

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		
		local Humanoid = Player.Character:WaitForChild("Humanoid")
		
		Humanoid.HealthChanged:Connect(function(Health)
			print(Health)
		end)
		
		repeat wait(1); Humanoid:TakeDamage(10) until Humanoid.Health == 0
		
	end)
end)

Result:

image

Make sure to double check that your script isn’t disabled and/or that you’re not setting the health on the client.

4 Likes

Allow me to correct myself, modifying the health value with the properties value does work, just as long as it’s being done on the server. The exact same goes for trying to set the healths value in a script, the event is listening on the server so it won’t fire if you’re setting it on the client, the health value doesn’t replicate.

2 Likes

Try this and see if it makes a difference

-- print whenever a player's health changes
local function addCharacter(character)
	local humanoid = character:waitForChild("Humanoid")	
	humanoid:getPropertyChangedSignal("Health"):connect(function()	
		print("changed")
	end)
end

local function addPlayer(player)
	-- add an existing character
	if player.character then
		addCharacter(player.character)
	end
	-- add the player's character as it respawns
	player.characterAdded:connect(addCharacter)
end

local players = game:GetService("Players")

-- add any existing players
for i, player in pairs(players:getPlayers()) do
	addPlayer(player)
end

-- add new players as they join
players.playerAdded:connect(function(player)
	addPlayer(player)
end)

-- keep in mind that this script won't print if you change health
-- on the client view in Accurate Play Solo, unless it is a localScript

This code might seem a little redundant, and it probably is if you are running it from a server script.

However, if you are running this from a local script, it is important to note that some players will have already been in the game when the script started running, and some of those players might already have characters. This code will account for that.

Edit: switched .Changed to :GetPropertyChangedSignal:

4 Likes

Why not use:

:GetPropertyChangedSignal("Health")

?

1 Like

Seems like an anomaly. HealthChanged should be firing in this case. What exactly have you tried so far and is this all the code you have? Is there anything in F9? Does the behaviour change if you reset? Requires a bit more details.

You’re right, that would be better to use. I’ll edit my post

1 Like

Works.

This issue seems to be happening only with HealthChanged and no other events (as far as I know)

But HealthChanged works perfectly fine on the client

Yes, however if you’re listening to .HealthChanged on the server and set the health from the client, it will not fire. From the looks of it that is what you’re doing, since the event has been working fine when running tests in studio environments. Correct me if I’m wrong however, and we can look into it further.

Assuming above is the case, what you want to do is set the health from the server.

It’d help find the issue if you could provide some source and more information on your current setup.

4 Likes