Create completely persistant vehicles

I’ve been reading for 6 hours straight on the forums to see if it’s possible yet nobody truly seems to know so am asking here.

Would it be possible to generate 30 cars that are saved on the database so they never despawn?

My idea is to save the car position aswell as player every 30 seconds and on player despawn make it still be there and if I restart the server on first user start spawn all cars with my unique characteristics, like plate, model and other details.

Would it be possible or am I losing time?

1 Like

Everything is possible as long as if you know how to make a rescoures/plugin with LUA

I know I can create cars and also know I can store their position by using a menu, will try to work on something this week.

I just started with lua 3 weeks ago so still learning.

2 Likes

Everyone starts somehwere :wink:

Yeah, read that after a while, my read posts went from 3k to 4.2k

This is what I managed to gather together so far, lots of research through the forums.

local carLoaded = false

RegisterNetEvent('persistent:onUserSpawn')
AddEventHandler('persistent:onUserSpawn', function()
	local user = GetPlayerPed(-1)
	if(not carLoaded)then
		Citizen.Wait(1500)
		copCar()
		Citizen.Wait(1)
		
		carLoaded = true
	end
end)

function copCar()

local x, y, z = nil
local myPed = GetPlayerPed(-1)
local player = PlayerId()

local vehicle = GetHashKey('police3')

    RequestModel(vehicle)

    while not HasModelLoaded(vehicle) do
    Wait(1)
    end

local spawned_car = CreateVehicle(vehicle, x, y, z, 181.814880371094, true, false)

    SetVehicleOnGroundProperly(spawned_car)
    SetVehicleNumberPlateText(spawned_car, "hypr9xa")
    SetModelAsNoLongerNeeded(vehicle)
    SetVehicleHasBeenOwnedByPlayer(spawned_car, true)

local id = NetworkGetNetworkIdFromEntity(spawned_car)
    SetNetworkIdCanMigrate(id, true)

end

Still learning everything I need to proceed with the creation of this.

1 Like

Any update on this? Would be great to have! Been looking for this!

I was messing around with it but realized GTA has a limit of consistent cars on the server thus it goes crazy.
Still trying to workaround that.

SetEntityAsMissionEntity to persist them

Remove SetEntityAsNoLongerNeeded.

Yeah, thing is even with that once more cars showed up they dissapear.

A server with like 1000 cars would be a complete chaos.

Well unfortunately you can’t really do persistant and not persistant

You either have to have the car spawned forever and delete it manually or have it tied to the GTA Engine to remove it randomly…

Well shit, if you can make it be there forever, that works to, cause its for a dealership, they still have to buy the car, so it would be cool to have them out to see!

So, waking this up again instead of making a new topic so people know where we’re at.

I want to build a survival server and I want cars to have trunk inventory but I don’t want them to be spawned or saved into a garage, I want it to be real life like, you leave your car in your yard, a neighbour comes and takes it and you’re fucked but you can recover it unless destroyed.

For now I just got the database structure which would be like:

carid(automatic id) plate(varchar) pos(varchar, x,y,z) state(if destroyed or not, to prevent spawning blowed up cars)

that would be for now and after that I would get the first connected player, if player joined trigger a server toggle that works once per server start, if the server reboots, it will only trigger on first join, that’s easy but what I find difficult is to sync them through players, so every player sees one car where it is, if more than a car is found it’s got to remove the dupe but how do you identify which one is the synced car.

Where I’m lost at is at GTA natives that could sync the cars.

The most “could work” thing I thought of is making it so if a player is on the server, no matter who it is, if he’s the first make him spawn the cars, sync them to all new players, not sure if GTA would allow transferring ownership of spawned cars so another player has it, the very next player could have the cars and sync them to the new.

I’m lost with the GTA net-work sync system since it wasn’t designed for this.

I’m really interested into this, I know it works on other platforms that work with SQL databases so if the engine is the same why would it not work here?

If someone could explain the GTA net-work sync system for entities I would highly appreciate!