Ragdoll Health?

I made a script that knocks people unconscious (Spawns a ragdoll when health is at 30 or below)
I would like to know how to add health to it. Like for example if I shoot it, it then sets the ragdoll health down and eventually sets a Boolean value true.
I really didnt understand how DarkRP did it, and it actually gave me errors when trying to use their code. If someone could write out a working script, I would be grateful ^^

I won’t write the code for you unless you really struggle with it, but I’ll tell you how I would do it.
First, create a EntityTakeDamage hook.
In the hook, you can get information about damage given to a particular entity. First, check if the entity is a ragdoll, and find the player associated with the ragdoll (I suggest you set a variable when you spawn it; ent.Player = ply).
Apply the damage to the player instead of just the ragdoll, using Entity:TakeDamageInfo, pass on the damageinfo from the hook.
Then get the damage from the damageinfo using DamageInfo:GetDamage
Check if the damage would cause the player’s health to drop to 0 or below, compare against the player’s health and handle the situation where the damage would cause the player to die, so you can remove the ragdoll if you want.

I’ll try to do it, I’ll message back if I get it to work :slight_smile:

[editline]7th September 2014[/editline]

I understand most of the code but how would the damage get passed to the player? thats what I’m stuck on :frowning:

[editline]7th September 2014[/editline]

I can’t get it to work. Theres no errors, it just wont work



function Unconscious(ply, ent)
    
	if ply:IsPlayer() then
    ragdoll = ents.Create("prop_ragdoll")
	ragdoll.Player = ply
	ragdoll:SetPos( ply:GetPos() )
	ragdoll:SetModel(ply:GetModel())
	ragdoll:Spawn()
	ragdoll:Activate()
	ragdoll:GetPhysicsObject():SetVelocity(ply:GetPhysicsObject():GetVelocity())
	
	ply:Spectate(OBS_MODE_IN_EYE)
	ply:SpectateEntity(ragdoll)
	ply.IsUnconc = true
	
	
	if ply.IsUnconc then
    ragdoll.ragHealth = 100
    end
	
	timer.Simple(0.01, function()
		if(ply:GetRagdollEntity() != nil and	ply:GetRagdollEntity():IsValid()) then
			ply:GetRagdollEntity():Remove()
		end
	end )
	end
end


function Gone( ply, ent )
   
   if ply:Health() <= 30 then
   
   Unconscious(ply)
    end
	
	if ply:Health() <= 0 then
	ply.Gone = true
	end
	
end
hook.Add("EntityTakeDamage", "TBCDMG", Gone)


function EntityTakeThatDamage( target, dmginfo )

    if ( target:GetClass() == "prop_ragdoll") then
 
		ragdoll.Player:TakeDamageInfo( CTakeDamageInfo:GetDamage( ) )

    end

end


The problem is you’re not using your EntityTakeThatDamage function as a hook.
First of all, if you’re using EnitityTakeDamage to check for player damage you should check if the entity is a player, and you should subtract the damage given from the players health because EntityTakeDamage is called before damage has been applied so that you can manipulate the damage.
Instead you should use the PlayerHurt hook like so, instead of your Gone and EntityTakeThatDamage functions try using this
[lua]hook.Add( “PlayerHurt”, “KnockUnconscious”, function( ply, _, healthRemaining )

if ply:Health() &lt;= 30 then Unconscious( ply ) end

end )

hook.Add( “EntityTakeDamage”, “RagdollDamage”, function( ent, dmg )
if ent:GetClass() ~= “prop_ragdoll” or not ent.Player then return end

ent.Player:TakeDamageInfo( dmg )

if ent.Player:Health() -dmg:GetDamage() &lt;= 0 then 
	-- player will die
end

end )[/lua]

Thanks so much but 1 problem…
ent is always returning nil even with ent in the brackets of the function. Im guessing its because ply is also there. Any alternatives to ent.Player = ply?

Do you mean in the EntityTakeDamage hook?

No, in the Unconscious function. The code that identifies ent.Player as the player. The bit you said to put in.

Oh sorry, I meant for you to put it on the ragdoll, so use
[lua]ragdoll.Player = ply[/lua]

It still doesn’t take health from the player when ragdoll is shot :frowning:



function Unconscious(ply)
    
	if ply:IsPlayer() then
    ragdoll = ents.Create("prop_ragdoll")
	ragdoll.Player = ply
	ragdoll:SetPos( ply:GetPos() )
	ragdoll:SetModel(ply:GetModel())
	ragdoll:Spawn()
	ragdoll:Activate()
	ragdoll:GetPhysicsObject():SetVelocity(ply:GetPhysicsObject():GetVelocity())
	
	ply:Spectate(OBS_MODE_IN_EYE)
	ply:SpectateEntity(ragdoll)
	ply.IsUnconc = true
	
	
	timer.Simple(0.01, function()
		if(ply:GetRagdollEntity() != nil and	ply:GetRagdollEntity():IsValid()) then
			ply:GetRagdollEntity():Remove()
		end
	end )
	end
end


hook.Add( "PlayerHurt", "KnockUnconscious", function( ply, _, healthRemaining )

	if ply:Health() <= 30 then Unconscious( ply ) end

end )

hook.Add( "EntityTakeDamage", "RagdollDamage", function( ent, dmg )
	if ent:GetClass() ~= "prop_ragdoll" or not ragdoll.Player then return end

	ragdoll.Player:TakeDamageInfo( dmg )

	if ragdoll.Player:Health() -dmg:GetDamage() <= 0 then 
		-- player will die
	end

end )


Don’t change it in the EntityTakeDamage, I only meant in your Unconscious function.

Ohh

[editline]7th September 2014[/editline]

It keeps saying: attempt to call field ‘Player’ (a userdata value)