Event that fires before humanoid dies

Hi All.

So I was trying to build a fighting game recently, and I came across an issue.
I wanted to run a function after a humanoid’s health is 0, but before the humanoid state is set to Dead. Does Roblox have any event similar to what I am describing? The humanoid.died event occurs when the humanoid actually dies, not before.

2 Likes

Well you can do

humanoid.Health.Changed:Connect(function()
    if humanoid.Health == 0 then
        --Code
    end
end)

This will run when the humanoids health changes and the if statement makes sure the health is 0 now

1 Like

I don’t think they do, you can check if there health is about 0, like if its below 5, then they maybe about to die.

Hope this helps. :slight_smile:

Well, that would never work, because if the humanoid takes 5+ damage, then the function is definitely not gonna run before the humanoid dies.

1 Like

What are you trying to do, there may be a different way to do it.

It will help me out.

Also, I just found out that doing humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false) doesn’t prevent the humanoid from ceasing action when its health reaches 0, so that might be helpful.

1 Like

Well, I’m trying to have some sort of move that happens when you take fatal damage. Not that uncommon really.

1 Like

If you are trying to do that, you can do what said, or you can do this:

oldhealth = 0

humanoid.Health.Changed:Connect(function()
    if (humaniod.Health - oldhealth) > 35 then
       --code here
    else
         oldhealth = humaniod.Health
   end

Hope this helps :slight_smile: (Sorry, not the best code)

That is certainly an interesting approach, but it doesn’t quite fit what I’m working towards. Thank you for the advice though.

1 Like

You can try setting BreakJointsOnDeath on the humanoid to false, and then possibly then do animations on the character once they die.

1 Like

You can disable the death state exactly as you’ve discovered. Simply run humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false) on both the client through a localscript as well as the server through a script.

When the health reaches 0 absolutely nothing will happen. You’ll continue walking around with 0 health as you did with 100 health. Here you can run all the code you want. Then when you’re ready simply re-enable the state and change the humanoid state to the death state.

If you want to take this opportunity to not use the default death mechanics you can make your own with custom animations and utilization of other states like Falling Down, GettingUp, etc.

5 Likes

That is an interesting solution. It might work, but it honestly feels a bit janky.

I would also use a BodyPosition with really high damping inside of the HumanoidRootPart along with a BodyGyro facing forward to keep the dead player in place.

Wait, but when I do humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false), my character cannot move. Don’t get me wrong, it’s better that they not move, but it is an inconsistency to your response.

Also, thanks for the details!

If you can’t move after setting your state, then either you have code somewhere else running that changes that, or you have not properly disabled the death state on the server and client.

I specifically make use of the disabled death state among disabling some other states to get a knock-out state in my game. Players cannot die in my game. But you can get KO’d and taken hostage.

Edit: It’s also possible that I’m setting the walkspeed. I don’t think I am though. Should be able to walk. In my implementation I have a sprint system so… I set the walk speed to make sure you’re not sprinting in a knock-out state.

1 Like

Huh. I actually didn’t set it on the client. Thanks for the help!

1 Like