• Ragdoll Health?
    10 replies, posted
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 [url=http://wiki.garrysmod.com/page/GM/EntityTakeDamage]EntityTakeDamage[/url] 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 [url=http://wiki.garrysmod.com/page/Entity/TakeDamageInfo]Entity:TakeDamageInfo[/url], pass on the damageinfo from the hook. Then get the damage from the damageinfo using [url=http://wiki.garrysmod.com/page/CTakeDamageInfo/GetDamage]DamageInfo:GetDamage[/url] Check if the damage would cause the player's health to drop to 0 or below, compare against [url=http://wiki.garrysmod.com/page/Entity/Health]the player's health[/url] 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 :) [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 :( [editline]7th September 2014[/editline] I can't get it to work. Theres no errors, it just wont work [code] 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 [/code]
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 [url=http://wiki.garrysmod.com/page/GM/PlayerHurt]PlayerHurt[/url] hook like so, instead of your Gone and EntityTakeThatDamage functions try using this [lua]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 ent.Player then return end ent.Player:TakeDamageInfo( dmg ) if ent.Player:Health() -dmg:GetDamage() <= 0 then -- player will die end end )[/lua]
[QUOTE=Internet1001;45914808]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 [url=http://wiki.garrysmod.com/page/GM/PlayerHurt]PlayerHurt[/url] hook like so, instead of your Gone and EntityTakeThatDamage functions try using this [lua]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 ent.Player then return end ent.Player:TakeDamageInfo( dmg ) if ent.Player:Health() -dmg:GetDamage() <= 0 then -- player will die end end )[/lua][/QUOTE] 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?
[QUOTE=extra.game;45915840]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?[/QUOTE] Do you mean in the EntityTakeDamage hook?
[QUOTE=Internet1001;45915879]Do you mean in the EntityTakeDamage hook?[/QUOTE] No, in the Unconscious function. The code that identifies ent.Player as the player. The bit you said to put in.
[QUOTE=extra.game;45915894]No, in the Unconscious function. The code that identifies ent.Player as the player. The bit you said to put in.[/QUOTE] Oh sorry, I meant for you to put it on the ragdoll, so use [lua]ragdoll.Player = ply[/lua]
[QUOTE=Internet1001;45915989]Oh sorry, I meant for you to put it on the ragdoll, so use [lua]ragdoll.Player = ply[/lua][/QUOTE] It still doesn't take health from the player when ragdoll is shot :( [code] 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 ) [/code]
Don't change it in the EntityTakeDamage, I only meant in your Unconscious function.
Ohh [editline]7th September 2014[/editline] [QUOTE=Internet1001;45916201]Don't change it in the EntityTakeDamage, I only meant in your Unconscious function.[/QUOTE] It keeps saying: attempt to call field 'Player' (a userdata value)
Sorry, you need to Log In to post a reply to this thread.