Vehicles Persisting After Server-Side Deletion

Problem

I’m facing an issue in FiveM related to the Entity Owner and the deletion of entities. Even though I clearly remove vehicles on the server-side and the DoesEntityExist function reports that they no longer exist, these vehicles persist and do not get deleted. The vehicles are spawned in a different routing bucket (where the entity lockdown is set to “inactive”).

Server-Side Script

Here is my server-side script in TypeScript, which sets the routing bucket and removes previous vehicles:

onNet("tynopia:garage:server:enter", async (floor: number = 1) => {
    const source = global.source;
    const sourceString = source.toString();
    
    // ...

    if (playerPedId) {
        const bucket = routingBucket + source;

        for (const vehicle of GetAllVehicles()) {
            let count = 0

            if (GetEntityRoutingBucket(vehicle) === bucket) {
                do {
                    DeleteEntity(vehicle);
                    await Delay(50);
                    console.log("Try Delete: " + count++)
                } while (DoesEntityExist(vehicle))
            }
        }

        // ...

        SetPlayerRoutingBucket(sourceString, bucket);
        SetRoutingBucketPopulationEnabled(bucket, false);
        SetRoutingBucketEntityLockdownMode(bucket, "inactive");

        // ...
    }
})
export function exitGarage(source: string, playerPedId?: number, coords?: Vector3): void {
    SetPlayerRoutingBucket(source, 0);
    // ...
}

Client-Side Code

This is my client-side code where I create the vehicle:

// ...
await requestModel(model);
const entity = CreateVehicle(model, coords[0], coords[1], coords[2], coords[3], true, true);
await delayEntityExist(entity);
SetModelAsNoLongerNeeded(model);
// ...
export async function requestModel(model: number | string) {
    RequestModel(model)

    while (!HasModelLoaded(model)) {
        await Delay(50);
    }
}
export async function delayEntityExist(entity: number): Promise<void> {
    while (!DoesEntityExist(entity)) {
        await Delay(50);
    }
}

Issue Video

https://github.com/citizenfx/fivem/assets/65678882/47faf6e4-d9d2-4b52-956d-f170684a7273

Expected Behavior

When I delete vehicles on the server-side, they should be removed entirely, and there should be no old vehicles persisting when new vehicles are spawned in a different routing bucket.

Actual Behavior

Old vehicles persist and do not get deleted even when the server-side logic indicates that they have been removed and DoesEntityExist reports that they no longer exist.

1 Like

I had a similar issue, using DeleteEntity on the server seems to be weirdly unreliable. It was difficult to reproduce reliably and I could never really pin down under what conditions would it not actually delete the entity.

We had created a GitHub issue and after a few months we found a temporary solution to this problem