Example: vRP support for playernames

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)
4 Likes

Good to see your not just adding a download link :slight_smile:

the name blinks so much i can barely see it. other than that good job

that’s due to a client-side trainer/other script trying to handle name tags too, in case of lambda you can disable it in misc settings :stuck_out_tongue:

ahh thanks. i have scripthook disabled. But i do use mellotrainer, so I’ll try turning that off in the settings. Thanks for replying so quick! :grinning:

Could a scripts that upon command, allows the user to enter their firstname and name for it to then display. I don’t know the most about coding but we are fundamentally looking for a script where names can be set by the client via command when joining the server and will be shown in the game instead of either the FiveM settings one or the Steam one. Is this something that would be used, and if it needs editing, what sort of things would we be looking at adding / changing? Cheers mate.