OneSync Functions
ESX.OneSync server functions for ESX Legacy. Find nearby players, peds, objects and vehicles and spawn server-owned entities with SpawnVehicle and SpawnPed.
ESX.OneSync groups server-side helpers that need OneSync: area searches that the server could otherwise not do without asking a client, and functions that spawn networked entities owned by the server. Use them when the server must decide where entities are, for example for anti-cheat checks, job vehicles or NPCs that should survive their creator leaving.
Overview
| Fact | Value |
|---|---|
| Side | Server |
| Requires | OneSync (set onesync on in server.cfg) |
| Area functions | Implemented in esx_lib (xLib.onesync), exposed on ESX.OneSync |
| Spawn functions | es_extended/server/modules/onesync.lua |
| Return values | Network IDs, not entity handles |
Players
GetPlayersInArea
ESX.OneSync.GetPlayersInArea(source, maxDistance, ignore, routingBucket)| Name | Type | Description |
|---|---|---|
source | number or vector3 | A player ID (uses their ped position) or coordinates. |
maxDistance | number? | Radius. Default 100. |
ignore | table? | { [playerId] = true } for players to skip. |
routingBucket | number? | Only include players in this routing bucket. |
Returns: array of { id, ped, coords, dist }. ped is the network ID of the player's ped. Empty table if nobody is in range.
local nearby = ESX.OneSync.GetPlayersInArea(source, 15.0, { [source] = true })
for i = 1, #nearby do
TriggerClientEvent("myscript:client:hearShot", nearby[i].id, nearby[i].dist)
endGetClosestPlayer
ESX.OneSync.GetClosestPlayer(source, maxDistance, ignore, routingBucket)Same parameters as GetPlayersInArea. When source is a player ID, that player is skipped.
Returns: a single { id, ped, coords, dist } table, or an empty table if no player is in range.
local closest = ESX.OneSync.GetClosestPlayer(source, 3.0)
if closest.id then
local xTarget = ESX.GetPlayerFromId(closest.id)
-- e.g. search or cuff xTarget
endEntities in an area
The model filter is an allow-list: when you pass { [joaat("model")] = true }, only entities with those models are returned.
GetPedsInArea
ESX.OneSync.GetPedsInArea(coords, maxDistance, modelFilter)| Name | Type | Description |
|---|---|---|
coords | vector3, table or number | Center, or a player ID to use their position. |
maxDistance | number? | Radius. Without it, every matching ped on the server is returned. |
modelFilter | table? | { [modelHash] = true }. Only these models are returned. |
Player peds are excluded.
Returns: number[], network IDs.
local peds = ESX.OneSync.GetPedsInArea(vector3(-47.0, -1757.0, 29.4), 30.0)
print(("%d NPCs near the store"):format(#peds))GetObjectsInArea
ESX.OneSync.GetObjectsInArea(coords, maxDistance, modelFilter)Same parameters and return value as GetPedsInArea, for objects.
local atms = ESX.OneSync.GetObjectsInArea(source, 5.0, { [`prop_atm_01`] = true })GetVehiclesInArea
ESX.OneSync.GetVehiclesInArea(coords, maxDistance, modelFilter)Same parameters and return value as GetPedsInArea, for vehicles. The built-in /dv command uses it.
for _, netId in ipairs(ESX.OneSync.GetVehiclesInArea(coords, 10.0)) do
local vehicle = NetworkGetEntityFromNetworkId(netId)
if DoesEntityExist(vehicle) then DeleteEntity(vehicle) end
endClosest entity
These search within a fixed radius of 100 units.
GetClosestPed
ESX.OneSync.GetClosestPed(coords, modelFilter)| Name | Type | Description |
|---|---|---|
coords | vector3, table or number | Center, or a player ID. |
modelFilter | table? | { [modelHash] = true } allow-list. |
Returns: netId, distance, coords. Player peds are skipped. If nothing is within 100 units, the distance stays 100 and the network ID does not point to a real entity, so check it with DoesEntityExist(NetworkGetEntityFromNetworkId(netId)).
GetClosestObject
ESX.OneSync.GetClosestObject(coords, modelFilter)Same as GetClosestPed, for objects.
GetClosestVehicle
ESX.OneSync.GetClosestVehicle(coords, modelFilter)Same as GetClosestPed, for vehicles.
local netId, distance = ESX.OneSync.GetClosestVehicle(source)
if distance < 5.0 then
local vehicle = NetworkGetEntityFromNetworkId(netId)
print("Closest plate: " .. GetVehicleNumberPlateText(vehicle))
endSpawning entities
All spawn functions work in two modes. With a callback, they return immediately and call cb(netId) later (cb(false) on failure). Without a callback, they yield the current thread and return the network ID; a failure then raises an error, so wrap the call in pcall if you need to handle it.
SpawnVehicle
ESX.OneSync.SpawnVehicle(model, coords, heading, properties, cb, vehicleType)| Name | Type | Description |
|---|---|---|
model | string or number | Model name or hash. |
coords | vector3 or table | Spawn position. |
heading | number | Heading. |
properties | table | Vehicle properties. Stored in the VehicleProperties entity state bag, which the client applies. Use {} for none. |
cb | function? | function(netId). You can also pass the vehicle type string here and skip the callback. |
vehicleType | string? | "automobile", "bike", "boat", "heli", "plane", "submarine", "trailer" or "train". Resolved with ESX.GetVehicleType when omitted. |
Uses CreateVehicleServerSetter and sets the orphan mode so the vehicle is not deleted when its owner leaves.
Returns: network ID when called without a callback.
local netId = ESX.OneSync.SpawnVehicle("police", vector3(452.0, -1019.0, 28.4), 90.0, { plate = "LSPD 01" }, "automobile")
local vehicle = NetworkGetEntityFromNetworkId(netId)
TaskWarpPedIntoVehicle(GetPlayerPed(source), vehicle, -1)Without vehicleType, ESX must ask a client for unknown models. If no player is online, the spawn fails. Pass the type for vehicles spawned at server start.
SpawnObject
ESX.OneSync.SpawnObject(model, coords, heading, cb)| Name | Type | Description |
|---|---|---|
model | string or number | Model name or hash. |
coords | vector3 or table | Position. |
heading | number | Heading. |
cb | function? | function(netId). |
Returns: network ID when called without a callback.
ESX.OneSync.SpawnObject("prop_roadcone02a", coords, 0.0, function(netId)
if not netId then return end
FreezeEntityPosition(NetworkGetEntityFromNetworkId(netId), true)
end)SpawnPed
ESX.OneSync.SpawnPed(model, coords, heading, cb)Same parameters as SpawnObject, creates a networked ped.
local netId = ESX.OneSync.SpawnPed("s_m_y_cop_01", vector3(441.0, -978.0, 30.7), 180.0)SpawnPedInVehicle
ESX.OneSync.SpawnPedInVehicle(model, vehicle, seat, cb)| Name | Type | Description |
|---|---|---|
model | string or number | Ped model. |
vehicle | number | Vehicle entity handle (not a network ID). |
seat | number | Seat index, -1 is the driver. |
cb | function? | function(netId). |
Returns: network ID of the ped when called without a callback.
local vehNet = ESX.OneSync.SpawnVehicle("taxi", coords, heading, {}, "automobile")
local vehicle = NetworkGetEntityFromNetworkId(vehNet)
ESX.OneSync.SpawnPedInVehicle("a_m_y_stlat_01", vehicle, -1)Common issues
- Working with the wrong handle: every function returns network IDs. Use
NetworkGetEntityFromNetworkIdbefore calling entity natives. - Entities in other dimensions: only the player functions filter by routing bucket. Area and closest-entity searches cover all buckets.
- Spawned ped or object disappears: only
SpawnVehiclecallsSetEntityOrphanMode(entity, 2). Call it yourself on peds and objects that must persist when no player is near.
Related
Frequently asked questions
Independent documentation
Last updated on
xPlayer
Complete ESX Legacy xPlayer reference. Every server-side player method for money, accounts, inventory, jobs, weapons, metadata and notifications, verified against 1.16.4.
Client Functions
Reference for ESX Legacy client functions such as ESX.IsPlayerLoaded, ESX.GetPlayerData, ESX.SearchInventory and ESX.SecureNetEvent, with examples.