The latest cfx-server-data
repository version comes with a resource called playernames
, which will display overhead name tags using the game’s native support for these.
This is an example showing how one can use the extensibility API to add vRP identity fields to the player name templates.
Firstly, set the templates to be server-sided in server.cfg:
start playernames # if not already there
set playernames_svTemplate "[{{id}}] {{firstname}} {{name}}"
set playernames_template "{{serverName}}"
Then, make a new resource with the following files:
__resource.lua:
server_script '@vrp/lib/utils.lua'
client_script 'playernames_vrp_cl.lua'
server_script 'playernames_vrp_sv.lua'
resource_manifest_version '05cfa83c-a124-4cfa-a768-c24a5811d8f9'
playernames_vrp_cl.lua
-- this will trigger on a resource restart
TriggerServerEvent('playernamvrp:getIdentity')
playernames_vrp_sv.lua
-- load vRP dependencies
local Proxy = module("vrp", "lib/Proxy")
local vRP = Proxy.getInterface("vRP")
-- store identities for each player
local identities = {}
-- the event!
AddEventHandler('playernames:extendContext', function(id, cb)
id = tonumber(id)
if not identities[id] then return end
-- the cb adds to the context
for k, v in pairs(identities[id]) do
cb(k, v)
end
end)
-- on-join event to set the source's identity
AddEventHandler("vRP:playerJoin", function(user_id,source,name,last_login)
vRP.getUserIdentity({user_id}, function(identity)
identities[source] = identity
end)
end)
-- for resource reloading
RegisterNetEvent('playernamvrp:getIdentity')
AddEventHandler('playernamvrp:getIdentity', function()
local id = source
local user_id = vRP.getUserId({id})
vRP.getUserIdentity({user_id, function(identity)
identities[id] = identity
end})
end)