Vehicle Persistence & Cleanup Script

Vehicle Persistence & Cleanup Script

:pushpin: Overview

This script prevents vehicles that have been used by a player from despawning while automatically cleaning up abandoned vehicles after a configurable period of inactivity. It ensures only vehicles that have been driven by players are tracked, preventing random parked AI vehicles from being affected.

:scroll: License

This project uses a custom license Do not redistribute or modify without permission. (See Github for details)

:wrench: Key Features

:one: Smart Vehicle Registration

  • Only tracks player-driven vehicles – A vehicle is registered when:
    • A player enters it as a driver
    • A player spawns it and interacts with it
  • Does NOT track:
    • AI/parked vehicles
    • Vehicles never entered by a player

:two: Automatic Cleanup with Warnings

  • Configurable idle timer (default: 30 mins)
    • Vehicles untouched for the full duration are removed
  • Warning messages (customizable intervals):
    • 2 mins, 1 min, and 30 sec warnings before cleanup
    • Only the vehicle owner sees warnings (not global spam)

:three: Manual Admin Control

  • /cleanupvehicles command
    • Forces immediate cleanup of all inactive vehicles
    • Requires ACE permission (command.cleanupvehicles)

:four: Performance Optimised

  • Low-impact server checks (every 30 sec by default)
  • Distance-based occupancy checks (50m radius)
  • No residual memory leaks – Cleanly removes tracked vehicles

:gear: How It Works

:small_blue_diamond: Vehicle Tracking Logic

  • A vehicle is registered only when a player enters it (client-side detection).
  • The server tracks:
    • Owner (who spawned/used it)
    • Last used time (resets if any player enters)
    • Warning status (ensures no duplicate warnings)

:small_blue_diamond: Cleanup Process

  1. Checks every 30 seconds for inactive vehicles.
  2. If a vehicle is unoccupied for 30 mins:
  • Sits through warning phases (2m, 1m, 30s).
  • Deletes only if still unused after the full timer.
  1. Active vehicles reset the timer – even brief use extends their life.

:small_blue_diamond: Admin Command

  • /cleanupvehicles → Instantly removes all vehicles meeting idle criteria.
  • Permission-restricted (adjustable in config).

:inbox_tray: Installation

  1. Add to resources/ folder
  2. Ensure in server.cfg:

ensure VehiclePersistence

  1. Customise settings in server.lua (timers, messages, permissions).

:white_check_mark: Why Use This Script?

:heavy_check_mark: No more disappearing player vehicles
:heavy_check_mark: Prevents server bloat from abandoned cars
:heavy_check_mark: Private warnings (no global spam)
:heavy_check_mark: Supports manual admin cleanup
:heavy_check_mark: Optimised for performance

Perfect for roleplay, freeroam, and economy servers! :red_car::dash:


:link: Download & Support

Code is accessible Yes
Subscription-based No
Lines (approximately) ~300
Requirements None
Support Yes
4 Likes

Hey :wave:

Looking through your code, the only thing you are doing is adding the vehicle network id to a table on client/server side for tracking with your custom “cleanup” system. But there is no actual despawn prevention (or proper respawn) for those vehicles. If they despawn before your timer is up, they will simply be gone.

I fail to see where the persistence comes into play here. This seems like it is just a “cleanup” system for player driven vehicles, not persistence itself :sweat_smile:

2 Likes

That’s True, I Suppose. This Is Still Only v1.0.0 And Only Came About After Some People Wanted Help On Their Server For A Vehicle Despawn Control Script, So I Whipped This One Up During An Afternoon Last Week. The Aim Is Also To Not Require Any Dependencies Of Any Kind.

I Am Looking At Expanding Functionality. I Will Add Your Points To My To-Do List.

Many Thanks, All Feedback Is Appreciated.

2 Likes

You could use statebags to keep it persistant.
As the previous comment stated - you’ll probably need an additional checker to ensure the vehicle is there.

This is just my opinion on how this script could be improved.

I would save the netid, coordinates & vehicle information after a player has entered a vehicle, then update the coordinates every X seconds while the vehicle is still moving, slow down the ticker when it’s finally stationary, and eventually stop the ticker when we are certain that it’s stationary. To save on performance we only want the ticker to run when the player is the driver of the vehicle, either through a separate thread that can stop, or run it very slowly when we are not driving the vehicle.

When a player gets close to the vehicle, compare it to your list of vehicles. If it’s not there - create it.

1 Like

That’s Interesting Input, much appreciated.

I’m Looking Over The Script Over The Next Few Weeks And Will Look At Possibly incorporating this Type Of Concept.

Many Thanks

1 Like