OnPlayerDamage: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (issue 6495 was fixed)
(Removed fixed issues)
 
(9 intermediate revisions by 7 users not shown)
Line 3: Line 3:
This event is triggered when a player is damaged, in any way.
This event is triggered when a player is damaged, in any way.


It should be noted that this event is not triggered when attacked by a team member if friendly fire is enabled.
*This event is not triggered when attacked by a team member if friendly fire is enabled.
 
*Canceling this event has no effect. Cancel the client-side event [[onClientPlayerDamage]] instead.
It should also be noted that canceling this event has no effect. Cancel the client-side event [[onClientPlayerDamage]] instead.
*onPlayerDamage doesn't trigger if the damage kills the player, [[onPlayerWasted]] is called instead.  


==Parameters==
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
player attacker, int attackerweapon, int bodypart, float loss
player attacker, int damage_causing, int bodypart, float loss
</syntaxhighlight>  
</syntaxhighlight>  


*'''attacker''': A player element representing the player who was the attacker.  If there was no attacker this returns false.
*'''attacker''': a [[player]] element representing the player who was the attacker.  If there was no attacker this returns false.
*'''attackerweapon''': An integer representing the weapon the attacker used to kill the player
*'''damage_causing''': an [[int]] representing the cause of damage, either a [[Weapons|attacker weapon]], or some other [[Damage Types|types of damage]].
*'''bodypart''': An integer representing the bodypart ID the player was hit on when he got damaged.
*'''bodypart''': an [[int]] representing the bodypart ID the player was hit on when he got damaged.
{{BodyParts}}
{{BodyParts}}
*'''loss''': A float representing the percentage of health the player lost.
*'''loss''': a [[float]] representing the percentage of health the player lost.


==Source==
==Source==
Line 24: Line 24:
This example causes an instant kill when a player is shot in the head, and announces it in the chatbox.
This example causes an instant kill when a player is shot in the head, and announces it in the chatbox.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--add an event handler for the onPlayerDamage event
function playerDamage_text ( attacker, weapon, bodypart, loss ) --when a player is damaged
function playerDamage_text ( attacker, weapon, bodypart, loss ) --when a player is damaged
if ( bodypart == 9 ) then -- if the body part is 9, i.e. the head
if ( bodypart == 9 and attacker and attacker ~= source ) then -- if the body part is 9 (head) and there is an attacker and it wasn't self damage
             outputChatBox ( "Headshot!", getRootElement (), 255, 170, 0 ) --output "Headshot" into the chatbox
             outputChatBox ( "Headshot!", attacker, 255, 170, 0 ) --output "Headshot" into the chatbox
    killPed ( source, attacker, weapon, bodypart ) -- and kill the player
    killPed ( source, attacker, weapon, bodypart ) -- and kill the player
end
end
end
end
addEventHandler ( "onPlayerDamage", getRootElement (), playerDamage_text )
addEventHandler ( "onPlayerDamage", root, playerDamage_text ) --add an event handler for the onPlayerDamage event
</syntaxhighlight>
</syntaxhighlight>
==Issues==
{{Issues|
{{Issue|6495|[Fixed in 1.3.3-5411] onPlayerDamage has wrong parameters if source on vehicle  }}
}}


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}

Latest revision as of 10:41, 30 January 2022

This event is triggered when a player is damaged, in any way.

  • This event is not triggered when attacked by a team member if friendly fire is enabled.
  • Canceling this event has no effect. Cancel the client-side event onClientPlayerDamage instead.
  • onPlayerDamage doesn't trigger if the damage kills the player, onPlayerWasted is called instead.

Parameters

player attacker, int damage_causing, int bodypart, float loss
  • attacker: a player element representing the player who was the attacker. If there was no attacker this returns false.
  • damage_causing: an int representing the cause of damage, either a attacker weapon, or some other types of damage.
  • bodypart: an int representing the bodypart ID the player was hit on when he got damaged.
  • 3: Torso
  • 4: Ass
  • 5: Left Arm
  • 6: Right Arm
  • 7: Left Leg
  • 8: Right Leg
  • 9: Head
  • loss: a float representing the percentage of health the player lost.

Source

The source of this event is the player who was damaged.

Example

This example causes an instant kill when a player is shot in the head, and announces it in the chatbox.

function playerDamage_text ( attacker, weapon, bodypart, loss ) --when a player is damaged
	if ( bodypart == 9 and attacker and attacker ~= source ) then -- if the body part is 9 (head) and there is an attacker and it wasn't self damage
            outputChatBox ( "Headshot!", attacker, 255, 170, 0 ) --output "Headshot" into the chatbox
	    killPed ( source, attacker, weapon, bodypart ) -- and kill the player
	end
end
addEventHandler ( "onPlayerDamage", root, playerDamage_text ) --add an event handler for the onPlayerDamage event

See Also

Player events


Event functions