ESX Server Callbacks — Complete Guide for FiveM Developers
ESX Server Callbacks — Complete Guide
Server Callbacks are ESX's most important mechanism, allowing clients to securely request information from the server and receive responses in return.
What is Server Callback?
The problem with FiveM is that the client and server work separately. The client cannot access the database directly. Server Callback solves this problem by having the client ask the server and wait for an answer.
Creating Server Callback (Server-side)
ESX.RegisterServerCallback('myScript:getPlayerData', function(source, cb, extraParam)
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then
cb(nil)
return
end
MySQL.Async.fetchAll('SELECT * FROM custom_data WHERE identifier = @id', {
['@id'] = xPlayer.identifier
}, function(result)
cb(result)
end)
end)
Usage (Client-side)
ESX.TriggerServerCallback('myScript:getPlayerData', function(result)
if result then
print('Got data:', json.encode(result))
end
end, optionalExtraParam)
Secure Pattern: Validate everything on the server side.
-- Server callback: Buy products
ESX.RegisterServerCallback('myScript:buyItem', function(source, cb, itemName)
local xPlayer = ESX.GetPlayerFromId(source)
-- Server pulls price from Config itself, not from Client.
local item = Config.ShopItems[itemName]
if not item then
cb(false, 'item is invalid')
return
end
if xPlayer.getMoney() < item.price then
cb(false, 'not enough money')
return
end
xPlayer.removeMoney(item.price)
xPlayer.addInventoryItem(itemName, 1)
cb(true, 'Purchased successfully')
end)
Precautions
- Don't trust the price from the Client — Server must fetch price from Config itself.
- Check xPlayer first —
if not xPlayer then cb(nil) return end - Always call cb() — If not called, the Client will always hang.
Summary
Server Callbacks is a basic pattern that every FiveM developer must understand, use correctly, and check every input on the Server side.
Related Articles
Breaking: GTA Online อัพเดท "Money Fronts" — FiveM Server Owners ต้องทำอะไร?
Rockstar ปล่อย GTA Online อัพเดท Money Fronts มีผลกระทบต่อ FiveM servers บางส่วน นี่คือสิ่งที่ต้องทำทันทีหลังอัพเดท
Community Spotlight: Script และ Projects ที่น่าสนใจจาก FiveM Community
รวม scripts, tools และ projects ที่โดดเด่นจาก FiveM community ในช่วงที่ผ่านมา ตั้งแต่ free resources ถึง open-source projects
txAdmin อัพเดทใหม่ — Dashboard, Diagnostics และ Ban System ที่ดีขึ้น
txAdmin ซึ่งตอนนี้เป็นส่วนหนึ่งของ Cfx.re อย่างเป็นทางการ ได้รับการอัพเดทครั้งใหญ่ มี features ใหม่ที่ทำให้ Server Management ง่ายขึ้นมาก