OnPlayerTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 14: Line 14:


==Example==  
==Example==  
<section name="Server" class="server" show="true">
<section class="server" name="Example 1 (Server)" show="true">
This example destroys a haystack when a player targets it
This example destroys a haystack when a player targets it.''' It only works with elements created by MTA. '''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onPlayerTarget ( targetElem )
 
    -- if the targeted object is a haystack (an object with model ID 3374) remove it from the game
addEventHandler ("onPlayerTarget", root,
    if getElementType ( targetElem ) == "object" and getElementModel ( targetElem ) == 3374 then
    function(targetElem)
        destroyElement ( targetElem )
        if (isElement(targetElem)) and (getElementType(targetElem)=="object") and (getElementModel(targetElem)==3374) then
            destroyElement (targetElem)
        end
     end
     end
end
)
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )    -- add above function as handler for targeting event
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 20:16, 15 June 2018

This event is triggered when a player targets an element with his crosshair while aiming, or simply facing it while standing close. It's triggered again when the player no longer targets anything.

Parameters

element targettedElement
  • targettedElement: the element the player is targetting. false if no element is being targetted anymore.

Source

The source of this event is the player that is targetting the element.

Example

Click to collapse [-]
Example 1 (Server)

This example destroys a haystack when a player targets it. It only works with elements created by MTA.


addEventHandler ("onPlayerTarget", root, 
    function(targetElem)
        if (isElement(targetElem)) and (getElementType(targetElem)=="object") and (getElementModel(targetElem)==3374) then
            destroyElement (targetElem)
        end
    end
)


See Also

Player events


Event functions