Scripting Examples
Complete, working examples you can use as starting points. Copy an example into its own resource, keep client and server responsibilities separate, and rename every public event before extending it. Validate all client-provided values on the server before connecting an example to money, inventory, permissions, or persistence.
Example 1: Simple Notification Resource
fxmanifest.lua
fx_version 'cerulean'
game 'gta5'
author 'Your Name'
description 'Simple notification resource'
version '1.0.0'
client_scripts {
'client.lua'
}
server_scripts {
'server.lua'
}server.lua
RegisterCommand('notify', function(source, args)
TriggerClientEvent('myresource:notify', source, {
message = table.concat(args, ' ')
})
end, false)client.lua
RegisterNetEvent('myresource:notify')
AddEventHandler('myresource:notify', function(data)
-- Show notification (using your preferred method)
print('Notification:', data.message)
end)Example 2: Simple Menu
client.lua
local menuOpen = false
RegisterCommand('menu', function()
menuOpen = not menuOpen
if menuOpen then
-- Open menu
print('Menu opened')
else
-- Close menu
print('Menu closed')
end
end, false)Example 3: Database Query
server.lua
RegisterCommand('getuser', function(source, args)
local userId = tonumber(args[1])
if not userId then
return
end
MySQL.query('SELECT * FROM users WHERE id = ?', {userId}, function(result)
if result[1] then
TriggerClientEvent('chat:addMessage', source, {
args = {'User:', result[1].name}
})
end
end)
end, false)