From 56d74f64050992bef9d18aa054c48cc782dd7858 Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Thu, 27 Apr 2023 22:06:05 -0500 Subject: [PATCH] New function for server vehicle spawning --- server/functions.lua | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 5931e4c..8b9e106 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -220,7 +220,8 @@ function QBCore.Functions.SpawnVehicle(source, model, coords, warp) local ped = GetPlayerPed(source) model = type(model) == 'string' and joaat(model) or model if not coords then coords = GetEntityCoords(ped) end - local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true) + local heading = coords.w and coords.w or 0.0 + local veh = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, true) while not DoesEntityExist(veh) do Wait(0) end if warp then while GetVehiclePedIsIn(ped) ~= veh do @@ -241,11 +242,34 @@ end ---@param coords vector ---@param warp boolean ---@return number -function QBCore.Functions.CreateVehicle(source, model, coords, warp) +function QBCore.Functions.CreateAutomobile(source, model, coords, warp) model = type(model) == 'string' and joaat(model) or model if not coords then coords = GetEntityCoords(GetPlayerPed(source)) end + local heading = coords.w and coords.w or 0.0 local CreateAutomobile = `CREATE_AUTOMOBILE` - local veh = Citizen.InvokeNative(CreateAutomobile, model, coords, coords.w, true, true) + local veh = Citizen.InvokeNative(CreateAutomobile, model, coords, heading, true, true) + while not DoesEntityExist(veh) do Wait(0) end + if warp then TaskWarpPedIntoVehicle(GetPlayerPed(source), veh, -1) end + return veh +end + +--- New & more reliable server side native for creating vehicles +---comment +---@param source any +---@param model any +---@param vehtype any +-- The appropriate vehicle type for the model info. +-- Can be one of automobile, bike, boat, heli, plane, submarine, trailer, and (potentially), train. +-- This should be the same type as the type field in vehicles.meta. +---@param coords vector +---@param warp boolean +---@return number +function QBCore.Functions.CreateVehicle(source, model, vehtype, coords, warp) + model = type(model) == 'string' and joaat(model) or model + vehtype = type(vehtype) == 'string' and tostring(vehtype) or vehtype + if not coords then coords = GetEntityCoords(GetPlayerPed(source)) end + local heading = coords.w and coords.w or 0.0 + local veh = CreateVehicleServerSetter(model, vehtype, coords, heading) while not DoesEntityExist(veh) do Wait(0) end if warp then TaskWarpPedIntoVehicle(GetPlayerPed(source), veh, -1) end return veh