---@param model string|number
---@param cb? fun(vehicle: number)
---@param coords? vector4 player position if not specified
---@param isnetworked? boolean defaults to true
---@param teleportInto boolean teleport player to driver seat if true
function RSGCore.Functions.SpawnVehicle(model, cb, coords, isnetworked, teleportInto)
local playerCoords = GetEntityCoords(cache.ped)
local combinedCoords = vec4(playerCoords.x, playerCoords.y, playerCoords.z, GetEntityHeading(cache.ped))
coords = type(coords) == 'table' and vec4(coords.x, coords.y, coords.z, coords.w or combinedCoords.w) or coords or combinedCoords
model = type(model) == 'string' and joaat(model) or model
if not IsModelInCdimage(model) then return end
isnetworked = isnetworked == nil or isnetworked
lib.requestModel(model)
local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, isnetworked, false)
local netid = NetworkGetNetworkIdFromEntity(veh)
SetVehicleHasBeenOwnedByPlayer(veh, true)
SetNetworkIdCanMigrate(netid, true)
SetModelAsNoLongerNeeded(model)
if teleportInto then TaskWarpPedIntoVehicle(cache.ped, veh, -1) end
if cb then cb(veh) end
end
--- Example
local coords = RSGCore.Functions.GetCoords(PlayerPedId()) -- Ensure `coords` is vector4 with a heading value
coords = vec4(coords.x, coords.y, coords.z, coords.w or GetEntityHeading(PlayerPedId()))
RSGCore.Functions.SpawnVehicle('CART01', function(veh)
SetEntityHeading(veh, coords.w) -- Set the heading of the vehicle
TaskWarpPedIntoVehicle(PlayerPedId(), veh, -1) -- Teleport player into the vehicle
end, coords, true)