Vehicle Related Functions
ESX.GetVehicle
Returns an instance of xVehicle for the given entity id.
---@param vehicleEntity number | integer
---@return xVehicle?
ESX.GetVehicle(vehicleEntity)
local xVehicle = ESX.GetVehicle(entity)
print(json.encode(xVehicle, { indent = true }))
ESX.GetVehicles
Returns an array containing all instance of xVehicles.
---@return xVehicle[]
ESX.GetVehicles()
local xVehicles = ESX.GetVehicles()
for i = 1, #xVehicles do
local xVehicle = xVehicles[i]
print(json.encode(xVehicle, { indent = true }))
end
ESX.CreateVehicle
Spawns a vehicle and returns the instance of xVehicle.
If the first argument is a number, it will attempt to spawn a vehicle from the database with a matching id.
local vehicleId = MySQL.scalar.await("SELECT id FROM owned_vehicles WHERE owner = ? LIMIT 1", { xPlayer.getIdentifier() })
if vehicleId then
local coords = xPlayer.getCoords()
local xVehicle = ESX.CreateVehicle(vehicleId, coords, coords.heading)
if xVehicle then
print(json.encode(xVehicle, { indent = true }))
end
end
info
If the first argument is a table and the owner
property is a player identifier, or the group
property is a string, the vehicle will be added to the database.
Omitting both owner
and group
properties together, creates a non-persistent vehicle.
- Player-Owned Persistent Vehicle
- Group-Owned Persistent Vehicle
- Non-Persistent Vehicle
local coords = xPlayer.getCoords()
local vehicle = ESX.CreateVehicle({
model = "italirsx",
owner = xPlayer.getIdentifier(),
}, coords, coords.heading)
local coords = xPlayer.getCoords()
local vehicle = ESX.CreateVehicle({
model = "italirsx",
group = "police",
}, coords, coords.heading)
local coords = xPlayer.getCoords()
local vehicle = ESX.CreateVehicle({
model = "italirsx",
}, coords, coords.heading)