[Release][OneSync][ESX/ExM] Persistent Vehicles

UPDATED STANDALONE VERSION

This was made for a ExM server, if there's interest I could make it standalone. It relies on the new server side OneSync functions so you need the latest version of fivem.

# esx-persistent-vehicles

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

## Requirements

> FiveM Version >=2443
> 
> OneSync - This mod will not work without OneSync.
> 
> esx_vehicleshop

## Installation

Download from the releases tab in [GitHub](https://github.com/enc0ded/esx-persistent-vehicles). Extract and place the esx-persistent-vehicles in your resources folder. Start the resource.

> start esx-persistent-vehicles

Check config.lua and ensure the details match your database schema.

## Usage

To make a vehicle persistent, pass it's license plate to the event below. This event cannot be called on the client with TriggerServerEvent, it must be called server side. Call this when a player takes a vehicle out of a garage for example.

> TriggerEvent('persistent-vehicles/register-vehicle', plate)

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', plate)

Before you shutdown your server you will need to save the vehicles to file. 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('esx-persistent-vehicles')

## Console

Cull persistent vehicles

> pv-cull [number of vehicles]

Unpersist all 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.
7 Likes

Very cool release, except I just want to put a disclaimer for anyone here to know from personal experience. This will cause lag if you use a large amount of ā€œcustomā€ vehicles as they are usually high poly, and would cause texture loss. It would work well if people had active mechanic services to pick up and tow vehicles away.

3 Likes

20 charrs

2 Likes

With this mod vehicles are still removed and only get respawned when a player enters the area again. It shouldnā€™t result in that many more vehicles being spawned at any one time. Also, if the number of vehicles gets too high it will prune some.

Why not just use ESX.SetVehicleProperties considering you are already using ESX and it has that function? Other than that it looks good :+1:

I would love to see it standalone. Iā€™ve got one of my own but iā€™m going to check yours for sure.

Same Here, thanks for your share. Iā€™ lookin for a persistent Vehicle for a long time to avoir this garage systems :frowning:

Would be lovely to see it Standalone (or just ESX) before, maybe one day, hoping to use the OneSync one :slight_smile:

Thanks anyway dude. Iā€™ll Keep an eye on your projet.

Is there some example on how to use it?

btw: The debug function doesnt work

If vehicle is spawned with advancedgarage, the server script dont recognize the plate, but if i buy the vehicle, it does recognize it

Did you enter the command in the server console? It wonā€™t work through chat. Plus it only shows information if you have registered vehicles.

What garage script are you using? You need to run the register command when you take out a vehicle. Add it to your garage script, your vehicle shop and any jobs that spawn vehicles.

EDIT: Iā€™ll make an example script that uses this resource. Check back on the forum later.

I have my own garage script. Im using TriggerEvent(ā€¦) server side when I spawn and store the vehicle, but if i rejoin the server, the vehicle is gone :C

Can you try and comment out

SetVehicleModKit(vehicle, 0)

from ESX.Game.SetVehicleProperties in es_extended.

Vehicle might take up to 3 seconds to be registered after you run that command. Did you wait that long before leaving? Turn on debugging and see if the vehicle is added to the list before you leave.

EDIT: I should add, this is done incase the vehicle crashes the client. This way it wonā€™t keep respawning the vehicle when the player rejoins.

It work perfect! thanks!

Everything works except the vehicle-data.json file

Sorry, but if i delete SetVehicleModKit(vehicle, 0) the script detect plate, but i cant put modifications in the car, like spoilers and other stuff

Try this version. https://github.com/enc0ded/enc0ded-persistent-vehicles

Still the issue that vehicles double spawn. any update ?

I have tried for days I cannot get this to work for the life of me, I know im new at this but I have no idea what i may be doing wrong

Iā€™ll be referring to the latest updated version:
It doesnā€™t work automatically on each vehicle you enter. Youā€™ll have to trigger it somehow.
You can trigger it on each vehicle you enter with this rough snippet, but then youā€™d need to forget them with another script if they need to be deleted:

Citizen.CreateThread(function() 
	while true do
		Wait(1000)
		local newvehicle = GetVehiclePedIsIn(PlayerPedId(), false)
		if GetPedInVehicleSeat(GetVehiclePedIsIn(PlayerPedId(), false), - 1) == PlayerPedId() then
			TriggerEvent('persistent-vehicles/register-vehicle', newvehicle)
		end
	end
end)

I use a command to ā€˜parkā€™ and ā€˜unparkā€™ vehicles:

RegisterCommand('park', function()
	local newvehicle = GetVehiclePedIsIn(PlayerPedId(), false)
	if GetPedInVehicleSeat(GetVehiclePedIsIn(PlayerPedId(), false), - 1) == PlayerPedId() then
		FreezeEntityPosition(newvehicle, true)
		SetVehicleEngineOn(newvehicle, false, true, true)
		TriggerEvent('persistent-vehicles/register-vehicle', newvehicle)
		ESX.ShowHelpNotification('Vehicle Parked')
	else
		ESX.ShowNotification('Not driving a vehicle')
	end
end)

RegisterCommand('unpark', function()
	local newvehicle = GetVehiclePedIsIn(PlayerPedId(), false)
	if GetPedInVehicleSeat(GetVehiclePedIsIn(PlayerPedId(), false), - 1) == PlayerPedId() then
		FreezeEntityPosition(newvehicle, false)
		SetVehicleEngineOn(newvehicle, true, true, true)
		TriggerEvent('persistent-vehicles/forget-vehicle', newvehicle)
		ESX.ShowHelpNotification('Vehicle UnParked')
	else
		ESX.ShowNotification('Not driving a vehicle')
	end
end)

Check vehicle-data.json after running the commands

Iā€™m getting some duplications also but this script is deprecated and I donā€™t expect support