From 5eb31940cadc9b47ce0f56118c2d7475f6c8ec74 Mon Sep 17 00:00:00 2001 From: Arctos2win Date: Wed, 27 Sep 2023 20:02:08 +0200 Subject: [PATCH 1/2] improve spawnvehicle and remove callback --- [core]/es_extended/client/functions.lua | 39 +++++++------------------ [core]/es_extended/server/onesync.lua | 8 ++--- 2 files changed, 13 insertions(+), 34 deletions(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index 63955e737..46afb208c 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -336,34 +336,17 @@ end function ESX.Game.SpawnObject(object, coords, cb, networked) networked = networked == nil and true or networked - if networked then - ESX.TriggerServerCallback('esx:Onesync:SpawnObject', function(NetworkID) - if cb then - local obj = NetworkGetEntityFromNetworkId(NetworkID) - local Tries = 0 - while not DoesEntityExist(obj) do - obj = NetworkGetEntityFromNetworkId(NetworkID) - Wait(0) - Tries = Tries + 1 - if Tries > 250 then - break - end - end - cb(obj) - end - end, object, coords, 0.0) - else - local model = type(object) == 'number' and object or joaat(object) - local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) - CreateThread(function() - ESX.Streaming.RequestModel(model) - - local obj = CreateObject(model, vector.xyz, networked, false, true) - if cb then - cb(obj) - end - end) - end + + local model = type(object) == 'number' and object or joaat(object) + local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) + CreateThread(function() + ESX.Streaming.RequestModel(model) + + local obj = CreateObject(model, vector.xyz, networked, false, true) + if cb then + cb(obj) + end + end) end function ESX.Game.SpawnLocalObject(object, coords, cb) diff --git a/[core]/es_extended/server/onesync.lua b/[core]/es_extended/server/onesync.lua index 9fefaa744..47a4dbf78 100644 --- a/[core]/es_extended/server/onesync.lua +++ b/[core]/es_extended/server/onesync.lua @@ -86,8 +86,8 @@ function ESX.OneSync.SpawnVehicle(model, coords, heading, properties, cb) local vehicleProperties = properties CreateThread(function() - local xPlayer = ESX.OneSync.GetClosestPlayer(coords, 300) - ESX.GetVehicleType(vehicleModel, xPlayer.id, function(vehicleType) + local players = GetPlayers() + ESX.GetVehicleType(vehicleModel, players[math.random(1, #players)], function(vehicleType) if vehicleType then local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords, heading) if not DoesEntityExist(createdVehicle) then @@ -224,7 +224,3 @@ end function ESX.OneSync.GetClosestVehicle(coords, modelFilter) return getClosestEntity(GetAllVehicles(), coords, modelFilter) end - -ESX.RegisterServerCallback("esx:Onesync:SpawnObject", function(_, cb, model, coords, heading) - ESX.OneSync.SpawnObject(model, coords, heading, cb) -end) From 00c0e84fd645f42be959b8654f1734f86a159588 Mon Sep 17 00:00:00 2001 From: Arctos2win Date: Wed, 27 Sep 2023 21:46:12 +0200 Subject: [PATCH 2/2] change vehicle spawn logic back. --- [core]/es_extended/server/onesync.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/[core]/es_extended/server/onesync.lua b/[core]/es_extended/server/onesync.lua index 47a4dbf78..0841b15b5 100644 --- a/[core]/es_extended/server/onesync.lua +++ b/[core]/es_extended/server/onesync.lua @@ -86,8 +86,8 @@ function ESX.OneSync.SpawnVehicle(model, coords, heading, properties, cb) local vehicleProperties = properties CreateThread(function() - local players = GetPlayers() - ESX.GetVehicleType(vehicleModel, players[math.random(1, #players)], function(vehicleType) + local xPlayer = ESX.OneSync.GetClosestPlayer(coords, 300) + ESX.GetVehicleType(vehicleModel, xPlayer.id, function(vehicleType) if vehicleType then local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords, heading) if not DoesEntityExist(createdVehicle) then @@ -223,4 +223,4 @@ end ---@return number entityId, number distance, vector3 coords function ESX.OneSync.GetClosestVehicle(coords, modelFilter) return getClosestEntity(GetAllVehicles(), coords, modelFilter) -end +end \ No newline at end of file