GetPlayerTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{{Server client function}}
__NOTOC__
__NOTOC__
This function is used to get the current element a [[player]] is targeting.
This function is used to get the element a [[player]] is currently targeting.


==Syntax==
==Syntax==
Line 8: Line 9:


===Required Arguments===
===Required Arguments===
*'''thePlayer''': The [[player]] whose target you want to retrieve.
*'''thePlayer:''' The [[player]] whose target you want to retrieve.


===Returns===
===Returns===
Line 19: Line 20:


==Example==
==Example==
This example blows up any vehicle a specified player targets (aims at)
This example blows up any vehicle a player targets (aims at).
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function playerTargetCheck ( thePlayer ) -- called on a timer
function playerTargetCheck ( )
  target = getPlayerTarget ( thePlayer ) -- get the current target of the player
    local target
  if ( target ) then -- if there was a target
    for i, thePlayer in ipairs ( getElementsByType("player") ) do  -- iterate over all players
    if ( getElementType ( target ) == "vehicle" ) then -- if the target is a vehicle
        target = getPlayerTarget ( thePlayer )                     -- get the target of the current player
      blowVehicle ( target ) -- blow the vehicle
        if ( target ) then                                         -- if there was a target
            if ( getElementType ( target ) == "vehicle" ) then     -- and the target is a vehicle
                blowVehicle ( target )                             -- blow it up
            end
        end
     end
     end
  end
end
end
setTimer ( playerTargetCheck, 1000, 0 )                            -- call the check function every second
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Player functions}}
{{Player functions}}

Revision as of 13:58, 19 August 2007

This function is used to get the element a player is currently targeting.

Syntax

element getPlayerTarget ( player thePlayer )

Required Arguments

  • thePlayer: The player whose target you want to retrieve.

Returns

Returns the element that's being targeted, or false if there isn't one.

This is only effective on physical GTA elements, namely:

  • Players
  • Vehicles
  • Objects

Example

This example blows up any vehicle a player targets (aims at).

function playerTargetCheck ( )
    local target
    for i, thePlayer in ipairs ( getElementsByType("player") ) do  -- iterate over all players
        target = getPlayerTarget ( thePlayer )                     -- get the target of the current player
        if ( target ) then                                         -- if there was a target
            if ( getElementType ( target ) == "vehicle" ) then     -- and the target is a vehicle
                blowVehicle ( target )                             -- blow it up
            end
        end
    end
end
setTimer ( playerTargetCheck, 1000, 0 )                            -- call the check function every second

See Also