Garry's Mod Wiki

Revision Difference

GM:EntityTakeDamage#511349

<function name="EntityTakeDamage" parent="GM" type="hook">⤶ <ishook>yes</ishook>⤶ <description>Called when an entity takes damage. You can modify all parts of the damage info in this hook.</description>⤶ <realm>Server</realm>⤶ <predicted>No</predicted>⤶ <args>⤶ <arg name="target" type="Entity">The entity taking damage</arg>⤶ <arg name="dmg" type="CTakeDamageInfo">Damage info</arg>⤶ </args>⤶ <rets>⤶ <ret name="" type="boolean">Return true to completely block the damage event</ret>⤶ </rets>⤶ </function>⤶ ⤶ <example>⤶ <description>Explosion damage is reduced to players only.</description>⤶ <code>⤶ hook.Add( "EntityTakeDamage", "EntityDamageExample", function( target, dmginfo )⤶ ⤶ if ( target:IsPlayer() and dmginfo:IsExplosionDamage() ) then⤶ ⤶ dmginfo:ScaleDamage( 0.5 ) // Damage is now half of what you would normally take.⤶ ⤶ end⤶ ⤶ end )⤶ </code>⤶ ⤶ </example>⤶ ⤶ ⤶ <example>⤶ <description>Players in vehicles takes halved damage.</description>⤶ <code>⤶ hook.Add( "EntityTakeDamage", "EntityDamageExample2", function( target, dmginfo )⤶ ⤶ if ( target:IsVehicle() ) then⤶ ⤶ local ply = target:GetDriver()⤶ if ( IsValid(ply) &amp;&amp; dmginfo:GetDamage() &amp;gt; 1 ) then⤶ dmginfo:SetDamage(dmginfo:GetDamage() / 2)⤶ ply:TakeDamageInfo(dmginfo)⤶ dmginfo:SetDamage(0)⤶ ⤶ end⤶ ⤶ end⤶ ⤶ end )⤶ </code>⤶ ⤶ </example>