Exports: sharing functions across resources
An export lets one resource borrow a function from another and get an answer back. That is the whole idea. Events fire and forget; exports are real function calls, so they run instantly and hand you a return value. This is how every framework wires its pieces together: exports.ox_inventory:GetItemCount, exports.es_extended:getSharedObject. Here is the deal: you will publish a function in one resource and then call it from a second one, so you actually see the cross-resource call this lesson is named for.
Build it
Make the two resource folders
Inside your server's resources folder, create both of these folders:
resources/qu_money
resources/qu_money_consumer
Create the files
Create this exact file layout:
resources/qu_money/
fxmanifest.lua
server.lua
resources/qu_money_consumer/
fxmanifest.lua
server.lua
Write qu_money/fxmanifest.lua
Open qu_money/fxmanifest.lua and paste this:
fx_version 'cerulean'
game 'gta5'
server_script 'server.lua'
Write qu_money/server.lua
Open qu_money/server.lua and paste this:
local balances = {}
local function getBalance(src)
balances[src] = balances[src] or 0
return balances[src]
end
local function addBalance(src, amount)
balances[src] = getBalance(src) + amount
return balances[src]
end
exports('GetBalance', getBalance)
exports('AddBalance', addBalance)
Write qu_money_consumer/fxmanifest.lua
Open qu_money_consumer/fxmanifest.lua and paste this:
fx_version 'cerulean'
game 'gta5'
dependencies {
'qu_money'
}
server_script 'server.lua'
Write qu_money_consumer/server.lua
Open qu_money_consumer/server.lua and paste this:
RegisterCommand('checkmoney', function(src)
exports.qu_money:AddBalance(src, 50)
local balance = exports.qu_money:GetBalance(src)
print('[qu_money_consumer] player ' .. src .. ' balance ' .. balance)
end, true)
Start and test it
Open server.cfg and add both lines, provider first:
ensure qu_money
ensure qu_money_consumer
Save. If this is the first time you are loading these resources, start them with ensure (provider first); restart only works on a resource that is already running. Run these in the txAdmin Live Console:
ensure qu_money
ensure qu_money_consumer
Later, after you edit a file, re-load with restart qu_money then restart qu_money_consumer.
Run this test from the txAdmin Live Console:
checkmoney
The 0 is the server console's source id (the console is always player 0). The 50 is the value that came back across the resource boundary from qu_money. Run checkmoney again and the balance climbs to 100, then 150. The number persists because both calls read and write the same balances table living inside qu_money.
Keep reading the full lesson
Sign in to start, then unlock every step of this lesson and the full FiveM School with a membership.
- How it works
- If something went wrong
- What you can do now
- Try it yourself
The remainder of Exports across resources is available to FiveM School members.