> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thunder-studio.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Examples

> Examples of how to integrate ts-persistence into other scripts.

When a vehicle is deleted by an admin command or stored in a garage, you may need to remove it from persistence so it doesn't respawn later.
Here are a few examples of how to accomplish this using the `ForgetVehicle` export.

## Delete Vehicle Command (`/dv`)

When you delete a vehicle manually using a command, you should remove it from persistence so it is no longer restored automatically.

<Tabs>
  <Tab title="QB Core">
    Add this line: `exports['ts-persistence']:ForgetVehicle(nil, QBCore.Functions.GetPlate(veh))`

    ```lua resources/[qb]/qb-core/client/events.lua theme={null}
        RegisterNetEvent('QBCore:Command:DeleteVehicle', function()
            local ped = PlayerPedId()
            local veh = GetVehiclePedIsUsing(ped)
            if veh ~= 0 then
                -- Add this line to forget the vehicle
                exports['ts-persistence']:ForgetVehicle(nil, QBCore.Functions.GetPlate(veh))
                
                SetEntityAsMissionEntity(veh, true, true)
                DeleteVehicle(veh)
            else
                local pcoords = GetEntityCoords(ped)
                local vehicles = GetGamePool('CVehicle')
                for _, v in pairs(vehicles) do
                    if #(pcoords - GetEntityCoords(v)) <= 5.0 then
                        -- Add this line to forget the vehicle
                        exports['ts-persistence']:ForgetVehicle(nil, QBCore.Functions.GetPlate(v))
                        
                        SetEntityAsMissionEntity(v, true, true)
                        DeleteVehicle(v)
                    end
                end
            end
        end)
    ```
  </Tab>

  <Tab title="ESX">
    Add these lines before `DeleteEntity(Vehicle)`:

    ```lua theme={null}
    local plate = GetVehicleNumberPlateText(Vehicle)
    if not plate or plate == "" then
        print("No plate found for vehicle " .. tostring(Vehicle))
    else
        local success = exports['ts-persistence']:ForgetVehicle(nil, plate, true)
        if not success then print("Failed to forget vehicle " .. tostring(Vehicle)) end
    end
    ```

    ```lua [core]\es_extended\server\modules\commands.lua:113-173 theme={null}
        ESX.RegisterCommand(
            { "cardel", "dv" },
            "admin",
            function(xPlayer, args)
                local ped = GetPlayerPed(xPlayer.source)
                local pedVehicle = GetVehiclePedIsIn(ped, false)

                if DoesEntityExist(pedVehicle) then
                    local plate = GetVehicleNumberPlateText(pedVehicle)
                    if not plate or plate == "" then
                        print("No plate found for vehicle " .. tostring(pedVehicle))
                    else
                        local success = exports['ts-persistence']:ForgetVehicle(nil, plate, true)
                        if not success then print("Failed to forget vehicle " .. tostring(pedVehicle)) end
                    end
                    DeleteEntity(pedVehicle)
                end

                local coords = GetEntityCoords(ped)
                local Vehicles = ESX.OneSync.GetVehiclesInArea(coords, tonumber(args.radius) or 5.0)
                for i = 1, #Vehicles do
                    local Vehicle = NetworkGetEntityFromNetworkId(Vehicles[i])
                    if DoesEntityExist(Vehicle) then

                        local plate = GetVehicleNumberPlateText(Vehicle)
                        if not plate or plate == "" then
                            print("No plate found for vehicle " .. tostring(Vehicle))
                        else
                            local success = exports['ts-persistence']:ForgetVehicle(nil, plate, true)
                            if not success then print("Failed to forget vehicle " .. tostring(Vehicle)) end
                        end

                        DeleteEntity(Vehicle)
                    end
                end
                if Config.AdminLogging then
                    ESX.DiscordLogFields("UserActions", "Delete Vehicle /dv Triggered!", "pink", {
                        { name = "Player", value = xPlayer and xPlayer.name or "Server Console", inline = true },
                        { name = "ID",     value = xPlayer and xPlayer.source or "Unknown ID",   inline = true },
                    })
                end
            end,
            false,
            {
                help = TranslateCap("command_cardel"),
                validate = false,
                arguments = {
                    { name = "radius", validate = false, help = TranslateCap("command_cardel_radius"), type = "number" },
                },
            }
        )
    ```
  </Tab>
</Tabs>

## Sync door lock status changes

When your keys script locks/unlocks a vehicle, also update `ts-persistence` with:

`exports['ts-persistence']:UpdateDoorStatus(vehicle, status)`

* `status = 2` to lock
* `status = 1` to unlock

Example with `lfKeys` (same integration pattern for any keys script):

```lua theme={null}
if locked == 1 or locked == 0 then
    -- your lock logic
    exports['ts-persistence']:UpdateDoorStatus(vehicle, 2)
elseif locked == 2 then
    -- your unlock logic
    exports['ts-persistence']:UpdateDoorStatus(vehicle, 1)
end
```

Important: the `lfKeys` snippet is only a reference.\
For any other keys script, call `UpdateDoorStatus` right after the lock status changes.

## Adding to a Garage System

If you are adding this script to your server, you will need to call the export when a vehicle is successfully stored in the garage so that it is no longer restored from persistence.

Here is an example of how it looks in the default `qb-garages` script when a vehicle is deposited:

<Tabs>
  <Tab title="QB Core">
    Add this line: `exports['ts-persistence']:ForgetVehicle(nil, plate)`

    ```lua resources/[qb]/qb-garages/client/main.lua theme={null}
        local function DepositVehicle(veh, data)
            local plate = QBCore.Functions.GetPlate(veh)
            QBCore.Functions.TriggerCallback('qb-garages:server:canDeposit', function(canDeposit)
                if canDeposit then
                    local bodyDamage = math.ceil(GetVehicleBodyHealth(veh))
                    local engineDamage = math.ceil(GetVehicleEngineHealth(veh))
                    local totalFuel = exports[Config.FuelResource]:GetFuel(veh)
                    TriggerServerEvent('qb-mechanicjob:server:SaveVehicleProps', QBCore.Functions.GetVehicleProperties(veh))
                    TriggerServerEvent('qb-garages:server:updateVehicleStats', plate, totalFuel, engineDamage, bodyDamage)
                    
                    -- Add this line to forget the vehicle from persistence
                    exports['ts-persistence']:ForgetVehicle(nil, plate)
                    
                    CheckPlayers(veh)
                    if plate then TriggerServerEvent('qb-garages:server:UpdateOutsideVehicle', plate, nil) end
                    QBCore.Functions.Notify(Lang:t('success.vehicle_parked'), 'primary', 4500)
                else
                    QBCore.Functions.Notify(Lang:t('error.not_owned'), 'error', 3500)
                end
            end, plate, data.type, data.indexgarage, 1)
        end
    ```
  </Tab>

  <Tab title="ESX">
    Add this line before `ESX.Game.DeleteVehicle(vehicle)`: `exports['ts-persistence']:ForgetVehicle(nil, ESX.Game.GetPlate(vehicle))`

    ```lua [esx_addons]\esx_garage\client\main.lua:344-359 theme={null}
        if isInVehicle then
            if IsControlJustReleased(0, 38) then
                local vehicle = GetVehiclePedIsIn(playerPed, false)
                local vehicleProps = ESX.Game.GetVehicleProperties(vehicle)
                ESX.TriggerServerCallback('esx_garage:checkVehicleOwner', function(owner)
                    if owner then
                        exports['ts-persistence']:ForgetVehicle(nil, ESX.Game.GetPlate(vehicle))
                        ESX.Game.DeleteVehicle(vehicle)
                        TriggerServerEvent('esx_garage:updateOwnedVehicle', true, currentMarker, nil,
                            {vehicleProps = vehicleProps})
                    else
                        ESX.ShowNotification(TranslateCap('not_owning_veh'), 'error')
                    end
                end, vehicleProps.plate)
            end
        end
    ```
  </Tab>
</Tabs>
