The problem is that I need to spawn vehicle in different locations on the map which not close enough to player and due of this vehicles spawns only when player is near the spawn position. For example, my character in Los Santos and I am trying to spawn vehicle in Sandy Shores. Vehicle will only appear when I will get close enough to spawn location. Is there are any method how can I spawn vehicle anywhere on the map?
Here are the code:
function SpawnMissionVehicle(modelName, coords, heading, canBeEntered)
local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName))
RequestModel(model)
while not HasModelLoaded(model) do
Citizen.Wait(0)
end
local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false)
local id = NetworkGetNetworkIdFromEntity(vehicle)
if canBeEntered then
SetNetworkIdCanMigrate(id, true)
else
SetNetworkIdCanMigrate(id, false)
SetVehicleAllowNoPassengersLockon(vehicle, true)
SetVehicleDoorCanBreak(vehicle, false)
SetVehicleDoorsLockedForAllPlayers(vehicle, true)
SetVehicleUndriveable(vehicle, true)
end
SetEntityAsMissionEntity(vehicle, true, false)
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
SetModelAsNoLongerNeeded(model)
RequestCollisionAtCoord(coords.x, coords.y, coords.z)
while not HasCollisionLoadedAroundEntity(vehicle) do
RequestCollisionAtCoord(coords.x, coords.x, coords.x)
Citizen.Wait(0)
end
return vehicle
end