getElementsWithinRange | Multi Theft Auto: Wiki Skip to content

getElementsWithinRange

Client-side
Server-side
Shared

Manual Review Required

Please finish this page using the corresponding Old Wiki article. Go to Contribution guidelines for more information.


This function is used to retrieve a list of all elements of specified type within a range of 3D coordinates.

Note

This function checks if elements are in a box, not in a sphere. This function doesn't work with elements which are created by createElement.

OOP Syntax Help! I don't understand this!

Syntax

table getElementsWithinRange ( float x, float y, float z, float range, [ string elemType = "" ], int interior, int dimension )
Required Arguments
  • x: the x coordinate at which to retrieve elements.
  • y: the y coordinate at which to retrieve elements.
  • z: the z coordinate at which to retrieve elements.
  • range: the range at the coordinates in which to retrieve elements.
  • interior: MISSING_PARAM_DESC
  • dimension: MISSING_PARAM_DESC
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • elemType (default: ""): The type of element you want a list of. This can be any element type, such as: "player": A player connected to the server. "ped": A ped. "vehicle": A vehicle. "object": An object. "pickup": A pickup. "marker": A marker.

Returns

  • table: value

Returns a table containing all the elements of the specified type within range. Returns an empty table if there are no elements within range. Returns false if the arguments are invalid.

Code Examples

shared

This example allows admins to destroy all vehicles in close proximity.

function deleteNearbyVehicles(playerElement)
local playerAccount = getPlayerAccount(playerElement)
if (not playerAccount) then
return false
end
local guestAccount = isGuestAccount(playerAccount)
if (guestAccount) then
return false
end
local accountName = getAccountName(playerAccount)
local aclObject = "user."..accountName
local adminGroup = aclGetGroup("Admin")
local playerAdmin = isObjectInACLGroup(aclObject, adminGroup)
if (not playerAdmin) then
return false
end
local playerX, playerY, playerZ = getElementPosition(playerElement)
local playerInterior = getElementInterior(playerElement)
local playerDimension = getElementDimension(playerElement)
local searchRange = 300
local nearbyVehicles = getElementsWithinRange(playerX, playerY, playerZ, searchRange, "vehicle", playerInterior, playerDimension)
for vehicleID = 1, #nearbyVehicles do
local vehicleElement = nearbyVehicles[vehicleID]
local validElement = isElement(vehicleElement)
if (validElement) then
destroyElement(vehicleElement)
end
end
end
addCommandHandler("deletenearbyvehs", deleteNearbyVehicles)

See Also

Element Functions