Need help with ESX Callbacks, or Callbacks in general

Hi there,

I’m not quite understanding the context of how the callbacks are supposed to work, could someone maybe explain to me when to use callbacks, and how to use em. Would highly appreciate it!

Callbacks are a handy way of getting data from the server without using multiple events.

Instead of this:

Client

function isPlateTaken(plate)
    TriggerServerEvent('event1', plate)
end

RegisterNetEvent('event2', function(taken)
    if taken then
        ESX.ShowNotification('Plate is taken')
    end
end

Server

RegisterNetEvent('event1', function(plate)
    local taken = MySQL.scalar.await('SELECT plate FROM owned_vehicles WHERE plate = ?', {plate})
    
    TriggerClientEvent('event2', source, taken)  -- return the data to the client
end

You can do it more simply with:

Client

function isPlateTaken(plate)
    ESX.TriggerServerCallback('event1', function(taken)
        if taken then
            ESX.ShowNotification('Plate is taken')
        end    
    end, plate)
end

Server

ESX.RegisterServerCallback('event1', function(source, cb, plate)
    local taken = MySQL.scalar.await('SELECT plate FROM owned_vehicles WHERE plate = ?', {plate})
    
    cb(taken) -- return the data to the client
end
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.