Skip to content
GAMPEONCreate
New project
Preview: this works today, but details can still change.

Exports

An export is a function that one resource offers to others on the same side.

bank/server.lua
local balances = {}
exports('GetBalance', function(playerId)
return balances[playerId] or 0
end)
exports('AddMoney', function(playerId, amount)
balances[playerId] = (balances[playerId] or 0) + amount
return balances[playerId]
end)
shop/server.lua
local money = exports.bank:GetBalance(source)
-- names with dashes: exports['my-bank']:GetBalance(source)

List the export names in the manifest so other developers find them:

bank/fxmanifest.lua
server_script 'server.lua'
server_exports { 'GetBalance', 'AddMoney' }
dependency 'bank' -- in the shop's manifest, so bank starts first

An export can return a table with functions, like ESX.GetPlayerFromId or QBCore.Functions.GetPlayer. The functions stay callable in the other resource and run in the resource that made them:

bank/server.lua
exports('GetAccount', function(playerId)
return {
balance = balances[playerId] or 0,
deposit = function(amount) balances[playerId] = (balances[playerId] or 0) + amount end,
}
end)
shop/server.lua
local account = exports.bank:GetAccount(source)
account.deposit(50) -- runs in bank
print(account.balance) -- a copy from the moment of the call: still the old value

Callbacks work the same way, also through local events: TriggerEvent('bank:getSharedObject', function(obj) Bank = obj end).

An export may call Wait, MySQL.*.await or other exports. The caller then waits for the result, as long as it runs in a thread or an event handler. Called from the main chunk of a script, the export still finishes, but its result is lost and the log says so. Wrap such calls in CreateThread.

  • Exports only work between resources on the same side.
  • Tables, strings, numbers and booleans are copied. Functions are passed as references and run in the resource they come from.
  • A function from a resource that stopped or restarted can no longer be called: function of resource 'bank' can no longer be called. Get it again after a restart.
  • Functions do not cross the network. In TriggerClientEvent, TriggerServerEvent and json.encode they become nil.
  • Calling an export of a resource that does not run is an error: No such resource 'bank' for export 'GetBalance'. Add a dependency.
  • resmon shows in the funcrefs column how many functions of a resource other resources hold. A number that only grows means a resource keeps references it no longer needs.

Report a problem with this pageOther ways to give feedback