Checking if an attribute has changed

for i, v in pairs(Character:GetChildren()) do
	if v:IsA("Part") and v:GetAttribute("Health") then -- All character parts excluding hrp
		v:GetAttributeChangedSignal("Health"):Connect(function()
			print("Health changed")
		end)
	end
end

After testing the code, it seems like this specific line v:GetAttributeChangedSignal("Health"):Connect(function() seems to be the problem, with print("Health changed") not printing out in output, although I don’t understand why this shouldn’t work.

5 Likes

That line looks good to me. Try testing some of these if you haven’t already:

  • Is there a part that satisfies that if statement? (Add a print(v) in the if statement and see if anything is printed)
  • Is Health the attribute or the property?
  • Does that attribute exist? (Maybe it was spelled wrong in the actual attribute)
  • Is the attribute changed? If so, is it changed after that line is reached?

Actually, there’s a specific event that will fire if the humanoid’s health changes. It’s under Humanoid class. I’ve used this myself for NPC bosses.

https://create.roblox.com/docs/reference/engine/classes/Humanoid#HealthChanged

Oh my god, I’m stupid, I forgot I had to test this with two players as one player would need to attack the other.