Your first RSG script#
This guide builds a small resource called rsg-hello from scratch. It registers a command, answers a callback from the client and shows notifications. It uses only rsg-core, so it needs no ox_lib.
The full reference for everything used here is in the rsg-core documentation.
1. Create the folder#
Put the resource next to the other rsg-* resources on your server:
rsg-hello/
fxmanifest.lua
config.lua
client/client.lua
server/server.lua
locales/en.json
2. Write the manifest#
fx_version 'cerulean'
rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aware my resources *will* become incompatible once RedM ships.'
game 'rdr3'
lua54 'yes'
description 'rsg-hello'
version '1.0.0'
shared_scripts {
'@rsg-core/shared/locale.lua', -- gives you locale()
'config.lua',
}
client_scripts { 'client/client.lua' }
server_scripts { 'server/server.lua' }
files { 'locales/*.json' }
dependencies { 'rsg-core' }
@rsg-core/shared/locale.lua replaces lib.locale(). The language comes from the rsg:locale convar in server.cfg, for example setr rsg:locale "en".
3. Add a locale file#
locales/en.json is the master file. Nested keys become dot keys, and %s placeholders work with string.format arguments.
{
"cmd_hello_desc": "Say hello",
"hello_title": "Hello",
"hello_body": "Welcome to the server, %s!",
"cash_title": "Your cash",
"cash_body": "You are carrying $%s."
}
Ship the same keys in every language your server supports: en, de, el, es, fr, ja, nl, pl, pt-br, ro and ru. Missing keys fall back to en.json, then to the key itself.
4. Get the core and register a command#
config.lua:
Config = {}
Config.CashCommand = 'mycash'
server/server.lua:
local RSGCore = exports['rsg-core']:GetCoreObject()
-- /hello
RSGCore.Commands.Add('hello', locale('cmd_hello_desc'), {}, false, function(source)
local Player = RSGCore.Functions.GetPlayer(source)
if not Player then return end
RSGCore.Functions.Notify(source, {
title = locale('hello_title'),
description = locale('hello_body', Player.PlayerData.charinfo.firstname),
type = 'success',
})
end)
Always check that Player exists before using it. The last argument of Commands.Add is an optional permission level such as 'admin'; leave it out for a command everyone can use.
5. Answer a callback from the client#
rsg-core callbacks answer with cb(...), not with return.
-- server/server.lua
RSGCore.Functions.CreateCallback('rsg-hello:server:getCash', function(source, cb)
local Player = RSGCore.Functions.GetPlayer(source)
cb(Player and Player.Functions.GetMoney('cash') or 0)
end)
-- client/client.lua
local RSGCore = exports['rsg-core']:GetCoreObject()
RegisterCommand(Config.CashCommand, function()
CreateThread(function()
local cash = RSGCore.Functions.AwaitCallback('rsg-hello:server:getCash', 5000)
if cash == nil then return end -- timed out
RSGCore.Functions.Notify({
title = locale('cash_title'),
description = locale('cash_body', cash),
type = 'inform',
})
end)
end, false)
AwaitCallback(name, timeout, ...) waits for the answer and returns nil on timeout. It yields, so call it from a thread or an event handler.
6. Start it#
Add the resource to server.cfg after rsg-core:
ensure rsg-core
ensure rsg-hello
Join the server and try /hello and /mycash.
Security checklist#
- Never trust numbers, prices, items or coordinates that arrive from the client. Work them out on the server from your config.
- Check the player's distance on the server with
GetEntityCoords(GetPlayerPed(source))before doing anything location-based. - Use
Player.Functions.AddMoneyandRemoveMoneyfor money, and check thatRemoveMoneysucceeded before handing over goods. - Use
?parameters with oxmysql, never string-built SQL.
Where next#
- Add an item and make it usable.
- Add a job with grades and pay.
- Add a target interaction to an NPC or zone.
- How the framework fits together.