How to disable dying at 0 health

Hi,
I need to make reviving system without making custom health system so I need to prevent player from dying at 0 health and attach my code which will run instead. Can someone help?

1 Like

Alright, I’m back at my PC and I’ve rewritten this post to contain more helpful information and more code to help you out with your endeavour. What I’ve got here now should work for you.

First, let’s disable the player dying on the server-side in a Script, preferably somewhere in ServerScriptService.

local Players = game:GetService("Players")

local function handleCharacter(character: any): ()
    local Humanoid = character:WaitForChild("Humanoid") :: Humanoid
    Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

    Humanoid.HealthChanged:Connect(function (health)
        if health > 0 then
            return
        end

        -- Replace with your desired Down But Not Out (DBNO) behavior!
        print(Players:GetPlayerFromCharacter(character).Name .. " was downed!")
    end)
end

local function handlePlayer(player: Player): ()
    local Character = if player.Character and player.Character.Parent
        then player.Character
        else player.CharacterAdded:Wait()

    handleCharacter(Character)
    player.CharacterAdded:Connect(handleCharacter)
end

for _, player in Players:GetPlayers() do
    handlePlayer(player)
end

Players.PlayerAdded:Connect(handlePlayer)

Now for the client-side, we need to do this for every player as well, because the client handles things like joint breakage itself.

local Players = game:GetService("Players")

local function handlePlayer(player: Player): ()
    local Character = if player.Character and player.Character.Parent
        then player.Character
        else player.CharacterAdded:Wait()

    local Humanoid = Character:WaitForChild("Humanoid") :: Humanoid
    Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

    player.CharacterAdded:Connect(function (character)
        -- I didn't think it was worth creating a function for 2 lines.
        local Humanoid = character:WaitForChild("Humanoid") :: Humanoid
        Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
    end)
end

-- This will include the Players.LocalPlayer.
for _, player in Players:GetPlayers() do
    handlePlayer(player)
end

Players.PlayerAdded:Connect(handlePlayer)

Some things for you to note:

  • If you wish to respawn the player, use Player:LoadCharacter(), because the game will no longer do this for you. This gives you more flexibility for when the player should be respawned (when you consider them dead).

  • You may want to handle health regeneration in your DBNO behavior, lest players gain health whilst downed.

  • A player killing themselves via client-side damage (e.g. resetting) will do nothing, you are welcome to handle this using a RemoteEvent or otherwise.

  • If you still want a player’s character to “break apart” you can use Model:BreakJoints() and set CanCollide to true on the player’s parts (otherwise the limbs etc. will fall through the map).

  • You should be able to adapt this code and use the workspace.DescendantAdded event to look for new Humanoids if you wish for it to apply all Humanoids and not just those owned by players.


Here’s an example of the code working!

7 Likes

Your code had a lot of mistakes so I corrected syntax error and SetStateType to SetStateEnabled but it didn’t work (zero affect on gameplay), I would send the corrected code but I already closed studio and my internet is bad, trust me I didn’t miss anything

1 Like

Player will see incorrect amount of health on the top right corner of the screen

1 Like

If you’re doing a custom revive logic, couldn’t you make a damage function yourself in a ModuleScript? You could have a function that checks if the player’s damage taken will be lethal, and instead of doing TakeDamage, it’ll just trigger the revive system.

2 Likes

@SuchTheLegendary I’ve updated my post here with some code that works, hope this helps!

2 Likes

I assume you have a module that takes away the health in your game. Just check the ending result before taking away the health. For example, Humanoid.Health currently equals to 10. And he touches something that would take away 15 health, do this first
result = Humanoid.Health - TakeAway
if result < 0 then
Humanoid.Health = Humanoid.Health + result – regenerate the health instead of taking away
– attach your code here!
end

3 Likes

Yes, let’s also make a custom engine that will handle my game better than roblox studio. The thing is that I want to keep this bar. I only need to attach other event to player’s death than the roblox’s default.

2 Likes

I want to swap the function that runs on event that roblox use to break joints, not use a module to prevent that event from firing @PrizeCounterPuncher same thing

2 Likes

Use Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

3 Likes

Edit: Nevermid it needs the server side

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.