Finding CURRENT Health of a humanoid?

Hello everyone! I have been messing around with collectionServices to optimize my game, as it has a lot of NPC enemies doing essentially the same thing. One issue I stumbled upon since then is that whenever I try to see the health of a humanoid for the NPC, it detects it’s standard starting health (in this case also MaxHealth) instead of how much health is left. Here is the code:

  local humanoi = v:FindFirstChild("Humanoi")
    if humanoi then
	print(v.Humanoi.Health)
	if humanoi.Health <= 0 then
-- and no, "Humanoi" is not a typo, it's to diffrentiate the NPCs from the Players for guns.

This is in a for i, v loop since it’s getting every memeber of the collectionservice. The print(v.Humanoi.Health) line of code prints “40”, which is how much health the NPC starts with, as opposed to how much is left. Thus the same applys for the if statement, and nothing can happen if the Humanoi dies.

And by the way, Humanoi.Died in this case does not work. It is because if you kill the zombie too early into it’s spawning, it will die but not change the value I want it to, meaning the game can’t continue. If you guys have any other substitutes for Humanoi.Died than this, also let me know.

Might not be a fix, but if you already defined humanoi then why would you need to do print(v.Humanoi.Health) instead of print(humanoi.Health)

Do you put this in a while true do loop or does it only run once? Because that would be the problem.

FindFirstChild returns a bool I think, I’ve tried it before and it returned nil instead of the humanoid.

If something returns nil, it doesn’t mean that it a bool. In this case, FindFirstChild should’t return a bool, it should return an object: the humanoid.

Try adding a script with while loop in the zombies that checks if their health is 0 or lower.

Also, if you want it to run more than once, yes, you would need to put it in some sort of loop.

1 Like

I assumed maybe that the fact that i was using a variable screwed things up, so I changed it to v.Humanoi.Health, but neither worked.

This runs every second under the collectionService script. A loop within that loop would cause one zombie to be correct then, but all others to freeze.

Perhaps it’s not an issue of the Health being printed wrong but rather how you’re changing the health. If the Health value is printing the starting health value, that means it hasn’t actually changed. Are you changing health from a LocalScript and trying to read it from a server script? If this is the case, then you’re facing expected behaviour.

Health is changed via a server script located within the gun you are using. I know that works fine because back when I was using Humanoid.Died as opposed to checking the health value, the zombies did indeed die. (Also if you check the Health Value in the Explorer, it is at 0, while the print(v.Humanoi.Health) line of code prints “40”.)

The issue with Humanoid.Died though is that if the zombie is killed before the collection Service script repeats while the zombie is alive (it repeats every one second to avoid lag, but it is somewhat common in the earlier waves for zombies to die within one second of spawning) then the zombie dies without executing the necessary function that follows for the round script to detect that the zombie has died, and this causes the game’s coding to essentially freeze since it’s still waiting on one more zombie to die.

My current solution is obviously checking the health value every time the collection service script is ran, but instead of getting the Zombie’s current health, it gets the health’s starting value.

Also I do not know if this is important (which it probably isn’t considering all other parts of this script work fine), but the zombies are clones of a zombie model located within Replicated Storage that is then moved to Workspace. Again, I don’t see that affecting this situation in any way, but it’s still a “just maybe” kind of thing.