[Release][OneSync] Enc0ded Persistent Vehicles

enc0ded-persistent-vehicles

[THIS VERSION IS DEPRECIATED AND NO LONGER SUPPORTED; PLEASE USE VERSION 2.0]

Version 2.0 can be found on our Tebex Store


This mod prevents vehicles from disappearing in OneSync multiplayer servers. It can also respawn vehicles in their previous location after a server restart.

Features

  • Respawns vehicles that have been unloaded by the game.
  • Respawns all vehicles that were out before server restart.
  • Performant: Only respawns vehicles when a player is closeby.
  • Entity Management: Delete vehicle entities (npc included) that are far away from all players.

Requirements

FiveM Version >=2443

OneSync - This mod will not work without OneSync.

Installation

Download from the releases tab in GitHub. Extract and place the enc0ded-persistent-vehicles folder in your resources folder. Start the resource.

start enc0ded-persistent-vehicles

Usage

To make a vehicle persistent, pass its entity to the event below in a client script. For example if you are using ESX you can put this in the call back of the ESX.Game.SpawnVehicle function.

TriggerEvent('persistent-vehicles/register-vehicle', entity)

Stop a vehicle from being persistent and allow it to be removed as normal. Does not delete the vehicle.
Call this when a player puts away a vehicle. Also remember to call this on your admin delete vehicle commands.

TriggerEvent('persistent-vehicles/forget-vehicle', entity)

If you enable repopulate on reboot then you need to call the server event below before your server shuts down. This will ensure that the vehicles spawn in the exact same location when the server comes back online.

TriggerEvent('persistent-vehicles/save-vehicles-to-file')

Alternatively you can stop the resource which will do this automatically.

StopResource('enc0ded-persistent-vehicles')

Console

Cull persistent vehicles

pv-cull <number of vehicles>

Unpersist vehicles

pv-forget-all

Toggle console debugging messages

pv-toggle-debugging

Save all persistent vehicles to file. Can be called before reboot.

pv-save-to-file

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

11 Likes

Good start for people who are interested in this concept. Nice release.
Bonus points for standalone!

Thanks for making this.

Thanks for entity detection!

1 Like

Does the script save the vehicles automatically on server stop?

This one doesnt work when called in the ESX.Game.DeleteVehicle() Event

Edit: @Nevos I just pushed the fix for that.

YOU ARE A GOD!!! THANK YOU FOR THIS SCRIPT!

I couldn’t figure out why it wasn’t working, then I saw you pushed a update, Nice bro! Very nice work!

1 Like

I get this Error:
SCRIPT ERROR: @enc0ded-persistent-vehicles/server/main.lua:187: attempt to index a nil value (local ‘props’)

handler (@enc0ded-persistent-vehicles/server/main.lua:13)

network_get_network_id_from_entity no such entity

I put TriggerEvent(‘persistent-vehicles/register-vehicle’, entity) in the ESX.Game.SpawnVehicle function (es_extended -> client -> functions.lua)
It looks like this:

ESX.Game.SpawnVehicle = function(modelName, coords, heading, cb)
	local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName))

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

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

		SetNetworkIdCanMigrate(networkId, true)
		SetEntityAsMissionEntity(vehicle, true, false)
		SetVehicleHasBeenOwnedByPlayer(vehicle, true)
		SetVehicleNeedsToBeHotwired(vehicle, false)
		SetVehRadioStation(vehicle, 'OFF')
		SetModelAsNoLongerNeeded(model)
		RequestCollisionAtCoord(coords.x, coords.y, coords.z)

		-- we can get stuck here if any of the axies are "invalid"
		while not HasCollisionLoadedAroundEntity(vehicle) and timeout < 2000 do
			Citizen.Wait(0)
			timeout = timeout + 1
		end

		if cb then
			cb(vehicle)
		end
	end)
	TriggerEvent('persistent-vehicles/register-vehicle', entity)
	print('spawn test')
end

The function needs to be inside that new thread.

ESX.Game.SpawnVehicle = function(modelName, coords, heading, cb)
	local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName))

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

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

		SetNetworkIdCanMigrate(networkId, true)
		SetEntityAsMissionEntity(vehicle, true, false)
		SetVehicleHasBeenOwnedByPlayer(vehicle, true)
		SetVehicleNeedsToBeHotwired(vehicle, false)
		SetVehRadioStation(vehicle, 'OFF')
		SetModelAsNoLongerNeeded(model)
		RequestCollisionAtCoord(coords.x, coords.y, coords.z)

		-- we can get stuck here if any of the axies are "invalid"
		while not HasCollisionLoadedAroundEntity(vehicle) and timeout < 2000 do
			Citizen.Wait(0)
			timeout = timeout + 1
		end
         TriggerEvent('persistent-vehicles/register-vehicle', vehicle )
		if cb then
			cb(vehicle)
		end
	end)
	
	print('spawn test')
end

If Someone could make this for those who don"t have OneSync and only ESX, would be great :wink:

Thanks anyway of course, for your work /bow

You can use OneSync it’s free for upto 32 players and use this one. Probably not gonna bother with the esx version now tbh.

1 Like

Oh ! Ok, don’t know this info :slight_smile:
Thanks Hatchett ! Gonna see that :slight_smile:

1 Like

Are you on the latest version?

I dont understand a thing. Does this script save NPC vehicles? Or player owned vehicles?

Any vehicle registered using the register-vehicle event. So it’s up to you.

Thanks, it works now! :slight_smile:

Can you help me where i can put this?
TriggerEvent(‘persistent-vehicles/save-vehicles-to-file’)

Depends on how your server is configured. Do you have a script that auto reboots the server?

No i´m using ZAP-Hosting. On the webpage i can setup the restart time.