From 1b2915a75f8b19c9dd163e66b2960753b40d1568 Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Fri, 22 Jul 2022 22:59:01 -0500 Subject: [PATCH] Vehicle server-spawning functions/callback --- server/events.lua | 11 +++++++++++ server/functions.lua | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/server/events.lua b/server/events.lua index ee8adfa..151d40a 100644 --- a/server/events.lua +++ b/server/events.lua @@ -244,3 +244,14 @@ QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, am end cb(retval) end) + +-- Vehicle server-side spawning callback (netId) +-- use the netid on the client with the NetworkGetEntityFromNetworkId native +-- convert it to a vehicle via the NetToVeh native +QBCore.Functions.CreateCallback('QBCore:Server:SpawnVehicle', function(source, cb, model, coords, warp) + if not coords then coords = GetEntityCoords(GetPlayerPed(source)) end + local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true) + while not DoesEntityExist(veh) do Wait(0) end + if warp then TaskWarpPedIntoVehicle(GetPlayerPed(source), veh, -1) end + cb(NetworkGetNetworkIdFromEntity(veh)) +end) \ No newline at end of file diff --git a/server/functions.lua b/server/functions.lua index eb2097a..cca2f8e 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -167,6 +167,30 @@ function QBCore.Functions.GetEntitiesInBucket(bucket --[[ int ]]) end end +-- Server side vehicle creation with optional callback +-- the CreateVehicle RPC still uses the client for creation so players must be near +function QBCore.Functions.SpawnVehicle(model, cb, coords, warp) + model = type(model) == 'string' and GetHashKey(model) or model + if not coords then coords = GetEntityCoords(GetPlayerPed(source)) end + local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true) + while not DoesEntityExist(veh) do Wait(0) end + if warp then TaskWarpPedIntoVehicle(GetPlayerPed(source), veh, -1) end + if cb then cb(veh) end +end + +-- Server side vehicle creation with optional callback +-- the CreateAutomobile native is still experimental but doesn't use client for creation +-- doesn't work for all vehicles! +function QBCore.Functions.CreateVehicle(model, cb, coords, warp) + model = type(model) == 'string' and GetHashKey(model) or model + if not coords then coords = GetEntityCoords(GetPlayerPed(source)) end + local CreateAutomobile = GetHashKey("CREATE_AUTOMOBILE") + local veh = Citizen.InvokeNative(CreateAutomobile, model, coords, coords.w, true, true) + while not DoesEntityExist(veh) do Wait(0) end + if warp then TaskWarpPedIntoVehicle(GetPlayerPed(source), veh, -1) end + if cb then cb(veh) end +end + -- Paychecks (standalone - don't touch) function PaycheckInterval()