ESX.Game
ESX.Game client reference for ESX Legacy - spawn and delete vehicles and objects, find the closest player or vehicle, and get or set vehicle properties.
ESX.Game is the client toolbox for entities: spawning and deleting vehicles and objects, finding the closest ped, player or vehicle, teleporting, and reading or applying vehicle tuning. In ESX Legacy 1.16 these functions live in esx_lib (xLib.game, xLib.entity) and es_extended maps them onto ESX.Game for compatibility.
Overview
| Resource | es_extended (implemented in esx_lib) |
| Side | Client |
| Mapping | es_extended/client/compat.lua |
| Implementation | esx_lib/imports/game/client.lua, esx_lib/imports/entity/client.lua |
coords parameters accept a vector3 or a table with x, y, z. modelFilter parameters are sets keyed by model hash, for example { [`prop_atm_01`] = true }.
Spawning and deleting
ESX.Game.SpawnVehicle
ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked)Loads the model, creates the vehicle, waits for collision and then returns it. The vehicle is unlocked for the player, does not need hotwiring and the radio is off.
| Name | Type | Description |
|---|---|---|
vehicleModel | string | number | Model name or hash. |
coords | vector3 | table | Spawn position. Must be within 424 units of the player. |
heading | number | Heading in degrees. |
cb | function? | function(vehicle). Without it the function waits and returns the vehicle. |
networked | boolean? | Default true. Pass true explicitly to also make the network id migratable and the vehicle a mission entity. |
Returns: number? vehicle handle when no cb is given.
local coords = GetOffsetFromEntityInWorldCoords(ESX.PlayerData.ped, 0.0, 5.0, 0.0)
local vehicle = ESX.Game.SpawnVehicle('sultan', coords, GetEntityHeading(ESX.PlayerData.ped))
TaskWarpPedIntoVehicle(ESX.PlayerData.ped, vehicle, -1)Invalid models raise an error (or reject the internal promise when you use the return value).
ESX.Game.SpawnLocalVehicle
ESX.Game.SpawnLocalVehicle(vehicleModel, coords, heading, cb)Same as SpawnVehicle with networked = false. Only the local player sees the vehicle, which is useful for showroom previews. Returns: nothing, use cb.
ESX.Game.SpawnObject
ESX.Game.SpawnObject(object, coords, cb, networked)| Name | Type | Description |
|---|---|---|
object | string | number | Model name or hash. |
coords | vector3 | table | Position. |
cb | function? | function(object). |
networked | boolean? | Default true. |
Returns: the object handle, or the return value of cb when you pass one.
local coords = GetEntityCoords(ESX.PlayerData.ped)
local cone = ESX.Game.SpawnObject('prop_roadcone02a', coords)
PlaceObjectOnGroundProperly(cone)ESX.Game.SpawnLocalObject
ESX.Game.SpawnLocalObject(object, coords, cb)Spawns a non-networked object. Returns: nothing, use cb.
ESX.Game.DeleteVehicle
ESX.Game.DeleteVehicle(vehicle)Marks the vehicle as mission entity and deletes it. The client must own the entity for the delete to replicate.
ESX.Game.DeleteObject
ESX.Game.DeleteObject(object)Marks the object as mission entity and deletes it.
ESX.Game.Teleport
ESX.Game.Teleport(entity, coords, cb)Requests collision at the target, waits until it is loaded, then moves the entity and sets its heading.
| Name | Type | Description |
|---|---|---|
entity | number | Entity handle, usually the player ped. |
coords | vector3 | vector4 | table | Target. Heading is read from w or heading, default 0.0. |
cb | function? | Called when done. |
ESX.Game.Teleport(ESX.PlayerData.ped, vector4(-1037.7, -2737.9, 20.2, 330.0), function()
ESX.ShowNotification('Welcome to Los Santos')
end)ESX.Game.GetPedMugshot
ESX.Game.GetPedMugshot(ped, transparent)Registers a ped headshot and waits until it is ready.
| Name | Type | Description |
|---|---|---|
ped | number | Ped handle. |
transparent | boolean? | Use a transparent background. |
Returns: mugshot handle and txd string, or nothing if the ped does not exist. Free it with UnregisterPedheadshot(mugshot) when you are done.
local mugshot, txd = ESX.Game.GetPedMugshot(ESX.PlayerData.ped)
ESX.ShowAdvancedNotification('LSPD', 'ID check', 'Identity verified', txd, 1, false, true)
UnregisterPedheadshot(mugshot)Entity pools
ESX.Game.GetObjects
ESX.Game.GetObjects()Returns: number[] from GetGamePool('CObject').
ESX.Game.GetPeds
ESX.Game.GetPeds(onlyOtherPeds)Returns: number[] from GetGamePool('CPed'), without the local player ped when onlyOtherPeds is true.
ESX.Game.GetVehicles
ESX.Game.GetVehicles()Returns: number[] from GetGamePool('CVehicle').
ESX.Game.GetPlayers
ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds)| Name | Type | Description |
|---|---|---|
onlyOtherPlayers | boolean? | Exclude the local player. |
returnKeyValue | boolean? | Return { [playerIndex] = ped }. |
returnPeds | boolean? | Return peds instead of player indexes (array mode only). |
Returns: table. Only players whose ped exists (in scope) are included.
Closest entity
All "closest" functions use the player position when coords is nil and return -1, -1 when nothing is found.
ESX.Game.GetClosestEntity
ESX.Game.GetClosestEntity(entities, isPlayerEntities, coords, modelFilter)| Name | Type | Description |
|---|---|---|
entities | table | Entity list. For players a { [playerIndex] = ped } table. |
isPlayerEntities | boolean | Return the table key (player index) instead of the entity. |
coords | vector3? | table? | Search origin. |
modelFilter | table? | Model hash set. |
Returns: entity, distance
ESX.Game.GetClosestObject
ESX.Game.GetClosestObject(coords, modelFilter)Returns: object handle and distance.
local atm, distance = ESX.Game.GetClosestObject(nil, {
[`prop_atm_01`] = true,
[`prop_atm_02`] = true,
[`prop_fleeca_atm`] = true,
})
if atm ~= -1 and distance < 1.5 then
OpenBankMenu()
endESX.Game.GetClosestPed
ESX.Game.GetClosestPed(coords, modelFilter)Returns: ped handle and distance. The local player ped is excluded.
ESX.Game.GetClosestPlayer
ESX.Game.GetClosestPlayer(coords)Returns: client player index and distance. The local player is excluded. There is no model filter.
local player, distance = ESX.Game.GetClosestPlayer()
if player ~= -1 and distance <= 3.0 then
TriggerServerEvent('police:handcuff', GetPlayerServerId(player))
else
ESX.ShowNotification('Nobody nearby', 'error')
endESX.Game.GetClosestVehicle
ESX.Game.GetClosestVehicle(coords, modelFilter)Returns: vehicle handle and distance.
ESX.Game.GetPlayersInArea
ESX.Game.GetPlayersInArea(coords, maxDistance)Returns: array of client player indexes within maxDistance (local player excluded).
ESX.Game.GetVehiclesInArea
ESX.Game.GetVehiclesInArea(coords, maxDistance)Returns: array of vehicle handles within maxDistance.
ESX.Game.IsSpawnPointClear
ESX.Game.IsSpawnPointClear(coords, maxDistance)Returns: true when no vehicle is within maxDistance of coords.
local spot = vector3(229.7, -800.1, 30.6)
if ESX.Game.IsSpawnPointClear(spot, 3.0) then
ESX.Game.SpawnVehicle('blista', spot, 160.0)
else
ESX.ShowNotification('The parking spot is blocked', 'error')
endESX.Game.GetVehicleInDirection
ESX.Game.GetVehicleInDirection()Casts a short ray from the camera.
Returns: vehicle, coords of the hit when the ray hits a vehicle, otherwise nothing.
ESX.Game.IsVehicleEmpty
ESX.Game.IsVehicleEmpty(vehicle)Returns: true when the vehicle has no passengers and the driver seat is free.
EnumerateEntitiesWithinDistance
EnumerateEntitiesWithinDistance(entities, isPlayerEntities, coords, maxDistance)Global helper (no ESX. prefix) that returns every entity (or player key) within maxDistance.
Vehicle properties
ESX.Game.GetVehicleProperties
ESX.Game.GetVehicleProperties(vehicle)Reads the full state of a vehicle into a table you can store in owned_vehicles.vehicle.
Returns: table|nil (nil if the vehicle does not exist).
| Group | Keys |
|---|---|
| Identity | model, plate (trimmed), plateIndex |
| Condition | bodyHealth, engineHealth, tankHealth, fuelLevel, oilLevel, dirtLevel, paintFade |
| Damage | doorsBroken, doorsAngleRatio, windowsBroken, tyreBurst, tyreBurstComplete, wheelHealth, wheelsBroken, tyresCanBurst, driftTyres |
| Paint | color1, color2 (number or RGB table), paintType1, paintType2, pearlescentColor, wheelColor, dashboardColor, interiorColor |
| Wheels and lights | wheels, wheelSize, wheelWidth, windowTint, xenonColor, customXenonColor, customXenonColorEnabled, neonEnabled, neonColor, tyreSmokeColor |
| Other | roofState, extras |
| Mods | modSpoilers ... modLightbar (one key per mod slot, for example modEngine, modBrakes, modTransmission, modSuspension, modArmor), toggles modNitrous, modTurbo, modSubwoofer, modSmokeEnabled, modHydraulics, modXenon, plus modCustomFrontWheels, modCustomBackWheels, modRoofLivery, modDoorR, modLivery, modLiveryIndex, livery |
Damage tables are keyed by index as string ('0', '1', ...), so they survive JSON encoding.
local vehicle = GetVehiclePedIsIn(ESX.PlayerData.ped, false)
local props = ESX.Game.GetVehicleProperties(vehicle)
if props then
TriggerServerEvent('garage:store', props.plate, props)
endESX.Game.SetVehicleProperties
ESX.Game.SetVehicleProperties(vehicle, props)Applies a properties table. Only keys present in props are changed. Sets the mod kit and waits up to 5 seconds for mods to load. Must run in a thread.
| Name | Type | Description |
|---|---|---|
vehicle | number | Vehicle handle. |
props | table | Properties as returned by GetVehicleProperties. |
local function spawnOwnedPreview(props)
local spawn = vector4(215.8, -810.1, 30.7, 157.0)
ESX.Game.SpawnVehicle(props.model, spawn.xyz, spawn.w, function(vehicle)
ESX.Game.SetVehicleProperties(vehicle, props)
end)
endFor vehicles spawned on the server, setting the VehicleProperties state bag on the entity makes the owning client apply the properties automatically.
Utilities
ESX.Game.Utils.DrawText3D
ESX.Game.Utils.DrawText3D(coords, text, size, font)Draws text at a world position, scaled by camera distance and FOV. Call it every frame.
| Name | Type | Description |
|---|---|---|
coords | vector3 | table | World position. |
text | string | Text. |
size | number? | Scale factor, default 1. |
font | number? | Font id, default 0. |
CreateThread(function()
local npc = vector3(-265.0, -963.6, 31.2)
while true do
local sleep = 500
if #(GetEntityCoords(ESX.PlayerData.ped) - npc) < 10.0 then
sleep = 0
ESX.Game.Utils.DrawText3D(npc + vector3(0.0, 0.0, 1.0), 'Job center')
end
Wait(sleep)
end
end)Removed raycast helpers
ESX.Game.GetShapeTestResultSync, ESX.Game.RaycastScreen and ESX.Game.StartRaycasting no longer exist in 1.16. ESX.Game.StopRaycasting, ESX.Game.IsRaycastActive and ESX.Game.GetRaycastResult remain only as compatibility stubs. Use the raycast module of esx_lib or the native StartShapeTestLosProbe instead.
Related
Frequently asked questions
Independent documentation
Last updated on
Callbacks
How ESX Legacy callbacks work: TriggerServerCallback, AwaitServerCallback, RegisterServerCallback and client callbacks, with timeouts and limits.
Interactions
Use ESX.RegisterInteraction, ESX.RemoveInteraction and ESX.GetInteractKey to bind world actions to the shared ESX interact key (E by default).