Snippet library#
Short, copy-paste snippets for common tasks. Each one uses the rsg-core and rsg-inventory APIs described in the rsg-core and rsg-inventory documentation. They assume this line at the top of the file:
local RSGCore = exports['rsg-core']:GetCoreObject()
Search every command, event and export in the reference.
Players#
Get a player and read their data (server)#
local Player = RSGCore.Functions.GetPlayer(source)
if not Player then return end
local citizenid = Player.PlayerData.citizenid
local first = Player.PlayerData.charinfo.firstname
local job = Player.PlayerData.job.name
Always check that Player exists. Other lookups: GetPlayerByCitizenId, GetOfflinePlayerByCitizenId and GetPlayerByCharInfo.
Players on duty for a job (server)#
local onDuty, count = RSGCore.Functions.GetPlayersOnDuty('vallaw')
for _, src in ipairs(onDuty) do
RSGCore.Functions.Notify(src, 'Alarm at the bank!', 'warning')
end
Nearest other player (server)#
local closestId, distance = RSGCore.Functions.GetClosestPlayer(source)
if closestId ~= -1 and distance < 3.0 then
-- close enough to interact
end
Money#
Add, remove and read money (server)#
Player.Functions.AddMoney('cash', 25, 'quest-reward')
if Player.Functions.RemoveMoney('cash', 10, 'shop-purchase') then
-- paid: hand over the goods
else
RSGCore.Functions.Notify(source, 'Not enough cash', 'error')
end
local cash = Player.Functions.GetMoney('cash')
Money types are cash, gold, bank and bloodmoney. Check the return value of RemoveMoney before giving anything in return. Do not add the money items (dollar, cent) yourself; always go through these functions.
Items#
Give an item only if it fits (server)#
if exports['rsg-inventory']:CanAddItem(source, 'bandage', 2) then
exports['rsg-inventory']:AddItem(source, 'bandage', 2, nil, nil, 'reward')
end
Check for and remove an item (server)#
if exports['rsg-inventory']:HasItem(source, 'bandage', 1) then
exports['rsg-inventory']:RemoveItem(source, 'bandage', 1, nil, 'used')
end
Make an item usable (server)#
RSGCore.Functions.CreateUseableItem('bandage', function(source, item)
local Player = RSGCore.Functions.GetPlayer(source)
if not Player then return end
-- item holds the slot data: name, amount, info, slot
end)
More in Add an item.
Jobs#
Check a held job and grade (server)#
local held, grade = exports['rsg-core']:HasPlayerJob(source, 'gunsmith')
if held and grade >= 1 then
-- allowed
end
Offer a job (server)#
local canTake, why = exports['rsg-core']:CanTakePlayerJob(source, 'taxi')
if not canTake then
return RSGCore.Functions.Notify(source, why, 'error')
end
exports['rsg-core']:AddPlayerJob(source, 'taxi', 0)
More in Add a job.
Notifications#
Notify from the client#
RSGCore.Functions.Notify({
title = 'Stable', description = 'Horse stored',
type = 'success', duration = 5000, icon = 'fa-solid fa-horse',
})
Notify from the server#
RSGCore.Functions.Notify(source, { title = 'Bank', description = 'Paid $5', type = 'success' })
Types are primary, inform, info, success, error and warning.
Callbacks#
Ask the server a question (client to server)#
-- server
RSGCore.Functions.CreateCallback('myres:server:getCash', function(source, cb)
local Player = RSGCore.Functions.GetPlayer(source)
cb(Player and Player.Functions.GetMoney('cash') or 0)
end)
-- client (inside a thread or event handler)
local cash = RSGCore.Functions.AwaitCallback('myres:server:getCash', 5000)
AwaitCallback returns nil on timeout. Handlers answer with cb(...), not return.
Ask a client a question (server to client)#
-- client
RSGCore.Functions.CreateClientCallback('myres:client:getHeading', function(cb)
cb(GetEntityHeading(PlayerPedId()))
end)
-- server
local heading = RSGCore.Functions.AwaitClientCallback('myres:client:getHeading', source, 5000)
Commands and keys#
Register a command with a permission (server)#
RSGCore.Commands.Add('announce', 'Send an announcement', {{ name = 'text', help = 'Message' }}, true, function(source, args)
-- args[1] ...
end, 'admin')
Register a rebindable key (client)#
RSGCore.Functions.RegisterKey('myres_open', locale('key_open'), 'I',
function() OpenMenu() end,
function() end,
{ canTrigger = function() return not IsPauseMenuActive() end })
The key is only a default. Players can rebind it in the game's key settings.
Progress and zones#
Progress bar with an animation (client)#
Call it from a thread, because it yields.
local done = RSGCore.Functions.ProgressBar({
duration = 5000,
label = 'Crafting',
canCancel = true,
anim = { dict = 'mech_inventory@crafting@fallbacks', clip = 'full_craft_and_stow', flag = 27 },
disable = { move = true, sprint = true, mount = true, combat = true },
})
if done then
-- finished
else
-- cancelled or interrupted
end
Sphere zone with enter and exit callbacks (client)#
Include the zones module in your manifest first:
client_scripts { '@rsg-core/client/zones.lua', 'client/*.lua' }
local zone = zones.sphere({
coords = vec3(0.0, 0.0, 0.0), radius = 2.0,
onEnter = function(self) end,
onExit = function(self) end,
})
-- later: zone:remove()
Targets#
exports['rsg-target']:addLocalEntity(ped, {
{ name = 'myres_talk', icon = 'fa-solid fa-comments', label = 'Talk', distance = 2.0,
onSelect = function(data) end },
})
More in Add a target interaction.