Add a target interaction#
rsg-target is the targeting system for RSG Framework. While the target key is held it finds the entity or zone you aim at and shows the options you registered, filtered by job, items and your own checks.
It is a rewrite of ox_target in the RSG resource layout and provides ox_target, so older scripts calling exports.ox_target keep working. In new code call exports['rsg-target'] and list 'rsg-target' in your manifest's dependencies. Do not run ox_target alongside it.
1. Add an option to a ped#
exports['rsg-target']:addLocalEntity(ped, {
{
name = 'rsg_hello_talk',
icon = 'fa-solid fa-comments',
label = locale('talk'),
distance = 2.0,
onSelect = function(data)
-- runs on the client when the player picks the option
end,
},
})
addLocalEntity is for an entity you spawned yourself. Use addEntity for networked entities, addModel for every entity of a model, and addGlobalPed, addGlobalVehicle, addGlobalObject or addGlobalPlayer for everything of a kind.
2. Restrict who sees it#
{
name = 'rsg_hello_trade',
icon = 'fa-solid fa-scale-balanced',
label = locale('trade'),
distance = 2.0,
groups = { trader = 0 }, -- job or gang name -> minimum grade
items = 'trade_licence', -- needs this item (rsg-inventory)
canInteract = function(entity, distance, coords, name)
return not IsPedDeadOrDying(entity, true)
end,
serverEvent = 'rsg-hello:server:trade',
}
Instead of onSelect an option can use event (client event), serverEvent, export or command.
3. Add a zone#
local id = exports['rsg-target']:addSphereZone({
coords = vec3(-278.0, 803.0, 119.0),
radius = 1.5,
options = {
{
name = 'rsg_hello_notice',
icon = 'fa-solid fa-clipboard',
label = locale('read_notice'),
onSelect = function() end,
},
},
})
addBoxZone and addPolyZone work the same way, and removeZone(id) removes a zone.
4. Always validate on the server#
Data that arrives through serverEvent comes from the client and can be forged. Check the player's job, items and distance on the server before acting:
local RSGCore = exports['rsg-core']:GetCoreObject()
RegisterNetEvent('rsg-hello:server:trade', function()
local src = source
local Player = RSGCore.Functions.GetPlayer(src)
if not Player then return end
-- check distance from the NPC, job grade and items here
end)
5. Clean up#
rsg-target removes a resource's options and zones automatically when that resource stops. Peds, props and anything else you spawned are still yours to delete:
AddEventHandler('onResourceStop', function(resource)
if resource ~= GetCurrentResourceName() then return end
if DoesEntityExist(ped) then DeleteEntity(ped) end
end)
Related#
- rsg-target documentation has the full option reference and handler payload.
- Migrating from ox_lib and ox_target