OnClientWeaponFire: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(15 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Client event}}
__NOTOC__  
__NOTOC__  
This event is triggered when a player fires a weapon near the local client.
This event triggers when a [[Element/Weapon|custom weapon]] fires a shot.
{{Note|This event is '''ONLY''' for custom weapons that were created with [[createWeapon]], for regular weapons use [[onClientPlayerWeaponFire]].}}
{{Note|This event is only triggered for custom weapons that are streamed in}}
==Parameters==
<syntaxhighlight lang="lua">
element hitElement, float posX,  float posY, float posZ, float normalX, float normalY, float normalZ, int materialType, float lighting, int pieceHit
</syntaxhighlight>
*'''hitElement:''' the element that was hit
*'''posX:''' the position it will hit
*'''posY:''' the position it will hit
*'''posZ:''' the position it will hit
*'''normalX:''' the normal it hit ( see processLineOfSight )
*'''normalY:''' the normal it hit ( see processLineOfSight )
*'''normalZ:''' the normal it hit ( see processLineOfSight )
*'''materialType:''' the material type it hit ( see processLineOfSight )
*'''lighting:''' the lighting of the entity it hit ( see processLineOfSight )
*'''pieceHit:''' the piece of the entity it hit ( see processLineOfSight )


==Syntax==  
==Source==
<syntaxhighlight lang="lua">
The [[event system#Event source|source]] of this event is the weapon that was fired.
void onClientWeaponFire ( int weapon, int ammo, int ammoInClip, float fX, float fY, float fZ, element hitElement )
</syntaxhighlight>


==Variables==
==Cancel Effect==
* The source of this event is the player which fired the gun.
If this event was [[Event system#Canceling|canceled]], then the weapon will not fire.
*'''weapon''': An integer representing the weapon that was fired.
*'''ammo''': An integer representing the player's remaining ammo.
*'''ammoInClip''': An integer representing the player's remaining ammo in clip.
*'''fX''': A floating point value representing the x coordinate of the hit.
*'''fY''': A floating point value representing the y coordinate of the hit.
*'''fZ''': A floating point value representing the z coordinate of the hit.
*'''hitElement''': Returns the element hit. Value is nil if nothing was hit.


==Example==  
==Example==  
This example calls server events, when an object was hit, or the player shot at something with a special gun.
This example prevents player damage from custom weapons.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--add an event handler for onClientWeaponFire
function noDamageToPlayersFromCustomWeapons(target)
addEventHandler ( "onClientWeaponFire", getRootElement (), "gunfire" )
    if target == localPlayer then
function gunfire ( weapon, ammo, ammoInClip, fX, fY, fZ, hitElement )
        cancelEvent() -- If the weapon hit the player, cancel the shot
        --something was hit?
    end
if ( hitElement ~= nil ) then
  triggerServerEvent( "object_hit" , getLocalPlayer () , hitElement )
        end
        -- filters if the player himself shot the gun, and if its weapon 33
if source == getLocalPlayer () and weapon == 33 then
  triggerServerEvent( "teleport_me" , getLocalPlayer () , fX, fY, fZ  )
        end
end
end
</syntaxhighlight>
addEventHandler("onClientWeaponFire", root, noDamageToPlayersFromCustomWeapons)</syntaxhighlight>


==See Also==
==See Also==
{{Event_functions}}
===Client event functions===
{{Client_event_functions}}
 
[[hu:onClientWeaponFire]]

Latest revision as of 17:30, 12 April 2019

This event triggers when a custom weapon fires a shot.

[[{{{image}}}|link=|]] Note: This event is ONLY for custom weapons that were created with createWeapon, for regular weapons use onClientPlayerWeaponFire.
[[{{{image}}}|link=|]] Note: This event is only triggered for custom weapons that are streamed in

Parameters

element hitElement, float posX,  float posY, float posZ, float normalX, float normalY, float normalZ, int materialType, float lighting, int pieceHit
  • hitElement: the element that was hit
  • posX: the position it will hit
  • posY: the position it will hit
  • posZ: the position it will hit
  • normalX: the normal it hit ( see processLineOfSight )
  • normalY: the normal it hit ( see processLineOfSight )
  • normalZ: the normal it hit ( see processLineOfSight )
  • materialType: the material type it hit ( see processLineOfSight )
  • lighting: the lighting of the entity it hit ( see processLineOfSight )
  • pieceHit: the piece of the entity it hit ( see processLineOfSight )

Source

The source of this event is the weapon that was fired.

Cancel Effect

If this event was canceled, then the weapon will not fire.

Example

This example prevents player damage from custom weapons.

function noDamageToPlayersFromCustomWeapons(target)
    if target == localPlayer then
        cancelEvent() -- If the weapon hit the player, cancel the shot
    end
end
addEventHandler("onClientWeaponFire", root, noDamageToPlayersFromCustomWeapons)

See Also

Client event functions