Exports
An export is a function that one resource offers to others on the same side.
local balances = {}
exports('GetBalance', function(playerId) return balances[playerId] or 0end)
exports('AddMoney', function(playerId, amount) balances[playerId] = (balances[playerId] or 0) + amount return balances[playerId]end)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:
server_script 'server.lua'server_exports { 'GetBalance', 'AddMoney' }dependency 'bank' -- in the shop's manifest, so bank starts firstObjects with methods
Section titled “Objects with methods”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:
exports('GetAccount', function(playerId) return { balance = balances[playerId] or 0, deposit = function(amount) balances[playerId] = (balances[playerId] or 0) + amount end, }end)local account = exports.bank:GetAccount(source)account.deposit(50) -- runs in bankprint(account.balance) -- a copy from the moment of the call: still the old valueCallbacks work the same way, also through local events: TriggerEvent('bank:getSharedObject', function(obj) Bank = obj end).
Waiting
Section titled “Waiting”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,TriggerServerEventandjson.encodethey becomenil. - Calling an export of a resource that does not run is an error:
No such resource 'bank' for export 'GetBalance'. Add adependency. resmonshows in thefuncrefscolumn how many functions of a resource other resources hold. A number that only grows means a resource keeps references it no longer needs.