Help with Player Death

Hello, i have made a somthing that when ever the player die to a specific weapon the Player gets revived after 5 Seconds but my problem is now that whenever the player die to somthing that isn’t one of the specific weapons the ragdoll dosen’t spawn. Here is the Code =

hook.Add(“DoPlayerDeath”, “Unique_Name”, function(victim, attacker, damageInfo)
local wep = attacker:GetActiveWeapon()
if ( IsValid( wep ) and wep:GetClass() == “weapon_lightsaber_training” or wep:GetClass() == “weapon_fists” ) then
timer.Simple(5, function()
if victim:IsValid() then
victim:revive()
end
end)
else
return
end
end)

This check is wrong:

if ( IsValid( wep ) and wep:GetClass() == “weapon_lightsaber_training” or wep:GetClass() == “weapon_fists” ) then

First, Player:GetActiveWeapon will always return an object, Weapon or NULL so you can replace IsValid(wep) by wep:IsValid (which is more efficient).

Next, your check is wrong, I see it more like this: wep:IsValid() and (wep:GetClass() == "class_1" or wep:GetClass() == "class_2"), even better, you can use a table to register all classes to match a result (see https://wiki.facepunch.com/gmod/Beginner_Tutorial_Tables).

So in the end, your code should look like this:

local classes = {
    ["weapon_lightsaber_training"] = true,
    ["weapon_fists"] = true,
}

hook.Add("DoPlayerDeath", "Unique_Name", function(victim, attacker, damageInfo)

    local weapon = attacker:GetActiveWeapon()

    if weapon:IsValid() and classes[weapon:GetClass()] then

        timer.Simple(5, function()

            if victim:IsValid() then

                victim:revive()

            end

        end)

    end

end)

Thx the code looks nicer now (Cleaner) but the Ragedoll still does not spawn

Maybe I misunderstood what you needed. If I have to take an example: a player dies from “weapon_fists”, then his body appears through the function “revive”?

I try to explain it to you. If i kill a Player with the weapon_fists the Ragedoll spawns. (I am using somthing that call the same function when a player get’s killed when an TFA Attachment is attacht because of this all TFA weapons spawn Ragedoll’s on the Player’s dead too) but when someone like get’s killed by Fall DMG the Ragedoll dosen’t spawn. This is my Problem + if i am deleting the thing with the TFA Attachment the Ragedoll only Spawn’s with weapon_fists

Yes, this is perfectly normal because the code checks if the attacker’s weapon is valid and if it’s a specific class. So you have to use the damageInfo argument of the hook with the function https://wiki.facepunch.com/gmod/CTakeDamageInfo:GetDamageType to detect if it’s a fall damage (DMG_FALL).

So the check could be: if (weapon:IsValid() and classes[weapon:GetClass()]) or damageInfo:GetDamageType() == DMG_FALL then

Blockquote

   attempt to call method 'GetActiveWeapon' (a nil value)

Here this is the error i get when a Player dies throu fall_DMG (The GetDamageType thing dosen’t work but thanks for you Help)

Yeah, because I forgot to check if the attacker is valid and if it’s a player. So it would look more like:

if (attacker:IsValid() and attacker:IsPlayer() and attacker:GetActiveWeapon():IsValid() and classes[attacker:GetActiveWeapon():GetClass()]) or damageInfo:GetDamageType() == DMG_FALL then