GetNearestPlayer2D: Difference between revisions

From Onset Developer Wiki
Created page with "{{Info|Function|Server|1.0}} {{FuncDescription|__EDIT_ME__}} {{FuncSyntax|GetNearestPlayer2D(x, y)}} {{FuncParameters}} {{FuncParam|x|__EDIT_ME__}} {{FuncParam|y|__EDIT_ME_..."
 
No edit summary
 
(One intermediate revision by one other user not shown)
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)}}


{{FuncParameters}}
{{FuncParameters}}
{{FuncParam|x|__EDIT_ME__}}
{{FuncParam|x|The X axis}}
{{FuncParam|y|__EDIT_ME__}}
{{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