fix(es_extended): dont spawn vehicle if non-existant

This commit is contained in:
Mycroft
2022-12-09 22:17:47 +00:00
parent bdd56329c7
commit 227532d4b6
2 changed files with 48 additions and 37 deletions
+25 -20
View File
@@ -690,28 +690,33 @@ AddEventHandler("esx:freezePlayer", function(input)
end)
RegisterNetEvent("esx:GetVehicleType", function(Model, Request)
local ReturnedType = "automobile"
local Model = Model
local VehicleType = GetVehicleClassFromName(Model)
local type = "automobile"
if VehicleType == 15 then
type = "heli"
elseif VehicleType == 16 then
type = "plane"
elseif VehicleType == 14 then
type = "boat"
elseif VehicleType == 11 then
type = "trailer"
elseif VehicleType == 21 then
type = "train"
elseif VehicleType == 13 or VehicleType == 8 then
type = "bike"
end
if Model == `submersible` or Model == `submersible2` then
type = "submarine"
end
TriggerServerEvent("esx:ReturnVehicleType", type, Request)
end)
local IsValidModel = IsModelInCdimage(Model)
if IsValidModel == true or IsValidModel == 1 then
local VehicleType = GetVehicleClassFromName(Model)
if VehicleType == 15 then
ReturnedType = "heli"
elseif VehicleType == 16 then
ReturnedType = "plane"
elseif VehicleType == 14 then
ReturnedType = "boat"
elseif VehicleType == 11 then
ReturnedType = "trailer"
elseif VehicleType == 21 then
ReturnedType = "train"
elseif VehicleType == 13 or VehicleType == 8 then
ReturnedType = "bike"
end
if Model == `submersible` or Model == `submersible2` then
ReturnedType = "submarine"
end
else
ReturnedType = false
end
TriggerServerEvent("esx:ReturnVehicleType", ReturnedType, Request)
end)
local DoNotUse = {
'essentialmode',
+23 -17
View File
@@ -61,23 +61,29 @@ end
---@param heading number
---@param Properties table
---@param cb function
function ESX.OneSync.SpawnVehicle(model, coords, heading, Properties, cb)
model = type(model) == 'string' and joaat(model) or model
Properties = Properties or {}
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
TriggerClientEvent("esx:requestModel", -1, model)
CreateThread(function()
local xPlayer = ESX.OneSync.GetClosestPlayer(vector, 200)
ESX.GetVehicleType(model, xPlayer.id, function(Type)
local SpawnedEntity = CreateVehicleServerSetter(model, Type, vector, heading)
Wait(250)
local NetworkId = NetworkGetNetworkIdFromEntity(SpawnedEntity)
Properties.NetId = NetworkId
Entity(SpawnedEntity).state:set('VehicleProperties', Properties, true)
cb(NetworkId)
end)
end)
end
function ESX.OneSync.SpawnVehicle(model, coords, heading, Properties, cb)
local veh_model = type(model) == 'string' and joaat(model) or model
Properties = Properties or {}
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
TriggerClientEvent("esx:requestModel", -1, model)
CreateThread(function()
local xPlayer = ESX.OneSync.GetClosestPlayer(vector, 300)
ESX.GetVehicleType(veh_model, xPlayer.id, function(Type)
if Type then
local SpawnedEntity = CreateVehicleServerSetter(veh_model, Type, vector, heading)
local NetworkId = NetworkGetNetworkIdFromEntity(SpawnedEntity)
while not DoesEntityExist(SpawnedEntity) do
Wait(100)
end
Properties.NetId = NetworkId
Entity(SpawnedEntity).state:set('VehicleProperties', Properties, true)
cb(NetworkId)
else
print(('[^1ERROR^7] Tried to spawn invalid vehicle - ^5%s^7!'):format(model))
end
end)
end)
end
---@param model number|string