Skip to content

Commit

Permalink
improve spawnvehicle and remove callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Arctos2win committed Sep 27, 2023
1 parent f904136 commit 5eb3194
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
39 changes: 11 additions & 28 deletions [core]/es_extended/client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions [core]/es_extended/server/onesync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 5eb3194

Please sign in to comment.