[How-To] Delete entities and vehicles from server-side

This is my first tutorial, not sure if it even fits here.
Oh well, I’m going to show you, well, spoon feed you the code to delete entities and vehicles from the server.

I’m not sure if this requires Onesync or not, feel free to correct me if I’m wrong, but I’m confident it does.
This works at the time of this post, if something died, feel free to comment I’ll help out.

All right, we’re past the boring stuff.
Time for the stuff you’re here for:

We’ll start with deleting entities…

RegisterNetEvent('anevent:deleteentity')
AddEventHandler('anevent:deleteentity', function()
    if hasPermission then -- Add permission check here.
        local coordFrom = GetEntityCoords(GetPlayerPed(source)) -- Get the position of the source.
        local info = {} -- Save a table so we can add the info here.
        for k,v in pairs(GetAllObjects()) do -- Do a loop of all the known server-side objects.
            local objCoord = GetEntityCoords(v) -- Get the coord of the object found
            local dist = #(coordFrom - objCoord) -- Find the distance

            if info.dist == nil and DoesEntityExist(v) then -- A little check here to make sure stuff doesn't break
                info.dist = dist
                info.obj = v
            end

            if DoesEntityExist(v) and info.dist > dist then -- Check if it exists and if it's closer to the player
                info.dist = dist -- Add it to the table yay
                info.obj = v -- Add the object too.
            end
        end

        if info.dist < 15 then -- Check if the object is closer than 15(something)
            DeleteEntity(info.obj) -- Delete the entity
            if DoesEntityExist(info.obj) then -- if it managed it...
                -- Notify player here using mythic notify probably.
            end
        else
            -- Notify player it's too far :((
        end
    end
end)

The above is all the code needed, I added comments to make it easier to understand.
You can do this with a lot of stuff actually, check out the natives with GetAll as the search.

I’ll show you how we do it with cars too, this time I’m too lazy to comment the code though:

RegisterNetEvent('anevent:deletevehicle')
AddEventHandler('anevent:deletevehicle', function()
    if haspermission then
        local coordFrom = GetEntityCoords(GetPlayerPed(source))
        local info = {}
        for k,v in pairs(GetAllVehicles()) do
            local objCoord = GetEntityCoords(v)
            local dist = #(coordFrom - objCoord)

            if info.dist == nil and DoesEntityExist(v) then
                info.dist = dist
                info.obj = v
            end

            if DoesEntityExist(v) and info.dist > dist then
                info.dist = dist
                info.obj = v
            end
        end

        if info.dist < 10 then
            DeleteEntity(info.obj)
            if DoesEntityExist(info.obj) then
                TriggerClientEvent('mythic_notify:client:SendAlert', source, { type = 'inform', text = 'Vehicle deleted on the server side.', length = 5000 })
            end
        else
            TriggerClientEvent('mythic_notify:client:SendAlert', source, { type = 'inform', text = 'Vehicle found, but it\'s too far.', length = 5000 })
        end
    end
end)

Of course, you have to make sure you trigger the event from something.

That should be all.
If you have any issues or expansions to this, feel free to leave a comment.

Thank you to: Indra for helping me out with this in #scripting channels on the Discord.

2 Likes