-- Define the location (Vector3) as a Lua table (X, Y, Z)
local targetLocation = {x = 123.45, y = 678.90, z = 1011.12}
-- Function to get the distance between two coordinates (Vector3) using 3D Pythagorean theorem
local function getDistance(coords1, coords2)
local distance = math.sqrt(
(coords1.x - coords2.x) ^ 2 +
(coords1.y - coords2.y) ^ 2 +
(coords1.z - coords2.z) ^ 2
)
return distance
end
-- Function to trigger event for players within a specified radius from a location (Vector3)
local function triggerEventForPlayersWithinRadius(eventName, location, radius)
local players = GetPlayers()
for _, player in ipairs(players) do
local playerCoordsTable = GetEntityCoords(GetPlayerPed(player))
-- Convert the fetched player coordinates (vector) to a Lua table (X, Y, Z)
local playerCoords = {x = playerCoordsTable.x, y = playerCoordsTable.y, z = playerCoordsTable.z}
local distance = getDistance(playerCoords, location)
-- If player is within the specified radius, trigger the event for that player
if distance <= radius then
TriggerClientEvent(eventName, player)
end
end
end
-- Example usage of the function to trigger a client event "yourEventName" for players within 30 meters from the specified location
triggerEventForPlayersWithinRadius("yourEventName", targetLocation, 30.0)
This example will read the locations of all players on the server, measure the distance to the target location, and trigger the client event for each player found within a 30-meter range.