GetNearestPlayer2D: Difference between revisions

From Onset Developer Wiki
28days (talk | contribs)
No edit summary
No edit summary
 
Line 1: Line 1:
{{Info|Function|Server|1.0}}
{{Info|Function|Server|1.0}}


{{FuncDescription|__EDIT_ME__}}
{{FuncDescription|Gets the closest player in the specified range of XY. An important function which replaces the use of looping through all players and then checking if they are the closest.}}


{{FuncSyntax|GetNearestPlayer2D(x, y)}}
{{FuncSyntax|GetNearestPlayer2D(x, y)}}
Line 9: Line 9:
{{FuncParam|y|The Y axis}}
{{FuncParam|y|The Y axis}}


{{FuncReturnValue|__EDIT_ME__}}
{{FuncReturnValue|Returns the player identifier or '''false'''}}


== Example ==
== Example ==
__EDIT_ME__
<syntaxhighlight lang="Lua">
AddCommand("closest", function(player)
  local x, y, z = GetPlayerLocation(player)
  local target = GetNearestPlayer2D(x, y)
 
  return AddPlayerChat(player, "Closest player is: " .. GetPlayerName(target))
end)
</syntaxhighlight>


{{RelatedFunctions}}
{{RelatedFunctions}}
__EDIT_ME__
{{ServerPlayerFunctions}}

Latest revision as of 14:41, 26 June 2021

GetNearestPlayer2D

Type: Function
Context: Server
Introduced: v1.0

Description

Gets the closest player in the specified range of XY. An important function which replaces the use of looping through all players and then checking if they are the closest.

Syntax

GetNearestPlayer2D(x, y)

Parameters

  • x
    The X axis
  • y
    The Y axis

Return Value

  • Returns the player identifier or false

Example

AddCommand("closest", function(player)
  local x, y, z = GetPlayerLocation(player)
  local target = GetNearestPlayer2D(x, y)

  return AddPlayerChat(player, "Closest player is: " .. GetPlayerName(target))
end)

See also