Not printing after enemy died?

I tried making a script where when you kill an enemy, for now it just prints that it is dead, but it doesn’t work, I have no clue about what to do. Here is my code so far.

while true do
		wait()
	if script.Parent.Humanoid.Health == nil then
		print("dead")
end

Health will never return nil, just do if humanoid.Health = 0
I recomend you using Humanoid.HealthChanged not a loop for this!

1 Like

how would I use a Humanoid.HealthChanged? Sorry, I am still new to scripting.

No… use Humanoid.Died actually.

Do this:

local humanoid = script.Parent.Humanoid

if humanoid.Health > 0 then humanoid.Died:Wait() end

print('dead')
1 Like

Thank you! I did not know I had to do “local humanoid = script.Parent.Humanoid”

You don’t have to - I just use a variable since I use it more than once, and putting script.Parent.Humanoid in multiple places is usually bad practice.

This line is the most important:

if humanoid.Health > 0 then humanoid.Died:Wait() end

If the humanoid is not already dead, wait for it to die. Then continue.

This catches cases where it’s already dead. It also doesn’t wait any longer than necessary for it to die.

1 Like

Uh, no that code wouldn’t work, as it’s just check also, he was checking for health that’s why I used Humanoid.HealthChanged, what if he wants to check if health is 50? uh?

Ok, so this topic is already closed and the poster confirmed that it works.

And he was checking if the player had died, not if the health had changed. That’s what has been provided

2 Likes

Health changed would work, to check if the player have died, using a constantly loop to checking would be inefficient, also, I was giving him a good way to do it, and don’t minimod, this has nothing to do you with you.

What terrodactyl said worked, thank you.

1 Like

Yes, but is a bad method, and you shouldn’t use a loop for that.

There’s no loop, there’s a single if statement. I don’t see the problem here. If it’s not dead, wait for it to die. Isn’t that the most straightforward way of making sure some humanoid is dead?

1 Like

He were using a loop, also Humanoid.Died:Wait()
If you an use Humanoid.Died function, doesn’t makes sense

It’s an if statement, first of all. Second of all, waiting for the Died event to fire is a perfectly acceptable way of waiting for a humanoid to die. What do you mean?

3 Likes