HumanTree92 / esx_advancedgarage

Advanced Garages for FiveM ESX Legacy
http://velocitientertainment.com/
68 stars 75 forks source link

[Question] Is it possible to make vehicles spawn in a way so they don't despawn when a player leaves / crashes? #236

Closed Paintsmoker closed 3 years ago

Paintsmoker commented 3 years ago

Describe the Bug | A Clear & Concise Description of Bug

I want to make the vehicle persistant. I looked for this for over a week now on any forum or any discord and no one knews who or wouldnt want to tell me. So can you maybe help with it? I want the car to be persistant when a player crashes until the server restarts.

Questions Have you made changes to anything in client/main.lua or server/main.lua?: No Have you looked through the Closed Topics?: Yes Have you looked through the Wiki?: Yes Are you using a Pre-Installed ESX? If Yes who?: Yes leagacy 1.19 Are you using es_extended? If Yes what Version? (Example: 1.2): Are you using Essentialmode?: Yes Are you using ExtendedMode?: No Are you using OneSync?: Yes Linux or Windows?: Windows (Own home pc)

Pesoen commented 3 years ago

i believe you need some kind of persistence flag when spawning them in, to make them stay, this needs to be done when the vehicle is spawned by the garage, and i cannot remember how, but it should be possible to find out in the fivem natives list here: https://docs.fivem.net/natives/

a quick look shows you need SetEntityAsMissionEntity

ghost commented 3 years ago
ESX.SpawnOwnedVehicle = function(modelName, coords, heading, cb)
    local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName))

    Citizen.CreateThread(function()
        ESX.Streaming.RequestModel(model)

        if coords == nil then
            local playerPed = PlayerPedId()
            coords = GetEntityCoords(playerPed) -- Prend la position du joueur playerPed
            heading = GetEntityHeading(playerPed)
        end

        local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false)
        local id = NetworkGetNetworkIdFromEntity(vehicle)

        SetNetworkIdCanMigrate(id, true)
        SetEntityAsMissionEntity(vehicle, true, false)
        SetVehicleHasBeenOwnedByPlayer(vehicle, true)
        SetVehicleNeedsToBeHotwired(vehicle, false)
        SetVehicleAutoRepairDisabled(vehicle, true)
        SetModelAsNoLongerNeeded(model)

        RequestCollisionAtCoord(coords.x, coords.y, coords.z)

        while not HasCollisionLoadedAroundEntity(vehicle) do
            RequestCollisionAtCoord(coords.x, coords.y, coords.z)
            Citizen.Wait(0)
        end

        SetVehRadioStation(vehicle, 'OFF')

        if cb then
            cb(vehicle)
        end
    end)
end

Play with this, had this for a long time. Make car alway stay even when player crash or disconnect.