Merge pull request #1799 from ASTROWwwW/fix/onesync-spawnvehicle

perf(es_extended): drop redundant player loops in OneSync.SpawnVehicle
This commit is contained in:
zykem
2026-07-21 14:25:26 +02:00
committed by GitHub
+13 -11
View File
@@ -114,25 +114,27 @@ function ESX.OneSync.SpawnVehicle(vehicleModel, coords, heading, vehicleProperti
CreateThread(function() CreateThread(function()
if not vehicleType then if not vehicleType then
local xPlayer = ESX.GetExtendedPlayers()[1] local playerId = next(ESX.Players)
if xPlayer then if playerId then
vehicleType = ESX.GetVehicleType(vehicleModel, xPlayer.source) vehicleType = ESX.GetVehicleType(vehicleModel, playerId)
end end
end end
if not vehicleType then if not vehicleType then
return reject("No players found nearby to check vehicle type! Alternatively, you can specify the vehicle type manually.") return reject("No players online to check vehicle type! Alternatively, you can specify the vehicle type manually.")
end end
local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords.x, coords.y, coords.z, heading) local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords.x, coords.y, coords.z, heading)
if not createdVehicle or createdVehicle == 0 then
return reject(("Could not spawn vehicle - ^5%s^7!"):format(vehicleModel))
end
-- the server owns the entity right after creation regardless of players
-- in scope, so a plain existence check is enough to know it is ready
local tries = 0 local tries = 0
while not DoesEntityExist(createdVehicle) do
local hasNetOwner = next(ESX.OneSync.GetClosestPlayer(coords, 300, nil, 0) or {}) ~= nil Wait(50)
while not createdVehicle or createdVehicle == 0
or (hasNetOwner and NetworkGetEntityOwner(createdVehicle) == -1)
or (not hasNetOwner and not DoesEntityExist(createdVehicle)) do
Wait(200)
tries = tries + 1 tries = tries + 1
if tries > 40 then if tries > 40 then
return reject(("Could not spawn vehicle - ^5%s^7!"):format(vehicleModel)) return reject(("Could not spawn vehicle - ^5%s^7!"):format(vehicleModel))