server script
local customnames = {}
RegisterCommand("changename", function(source, args)
if not args[2] then
print("Command requires two arguements. /changename <firstname> <lastname>")
return
end
local serverID = source
local fullname = args[1] .. ' ' .. args[2]
customnames[serverID] = fullname
TriggerClientEvent("namechange", -1, customnames)
end, false)
RegisterServerEvent("getcustomnames")
AddEventHandler("getcustomnames", function(source)
TriggerClientEvent("namechange", source, customnames)
end)
client script
local customnames = {}
Citizen.CreateThread(function()
while not NetworkIsPlayerActive(PlayerId()) do
Citizen.Wait(0)
end
TriggerServerEvent("getcustomnames")
end)
RegisterNetEvent("namechange")
AddEventHandler("namechange", function(names)
customnames = names
end)
function GetCustomNames()
return customnames
end
GetCustomNames() can be an export function set in your __resource.lua file which other resources like a modified scoreboard resource would pickup and display instead of steam or account name.
exports {"GetCustomNames"}
You can then just copy the existing scoreboard resource from [system] and name the copied folder to customscoreboard and have that start in your server.cfg instead of scoreboard. You would modify the scoreboard.lua file to look like below and it will pickup the custom names if they exist, else it would use the default steam or account name.
local listOn = false
Citizen.CreateThread(function()
listOn = false
while true do
Wait(0)
if IsControlPressed(0, 27)--[[ INPUT_PHONE ]] then
if not listOn then
local players = {}
local servernames = exports.customnames:GetCustomNames()
ptable = GetPlayers()
for _, i in ipairs(ptable) do
local wantedLevel = GetPlayerWantedLevel(i)
r, g, b = GetPlayerRgbColour(i)
local serverID = GetPlayerServerId(i)
local playerName = servernames[serverID]
if not playerName then
playerName = sanitize(GetPlayerName(i))
end
table.insert(players,
'<tr style=\"color: rgb(' .. r .. ', ' .. g .. ', ' .. b .. ')\"><td>' .. serverID .. '</td><td>' .. playerName .. '</td><td>' .. (wantedLevel and wantedLevel or tostring(0)) .. '</td></tr>'
)
end
SendNUIMessage({ text = table.concat(players) })
listOn = true
while listOn do
Wait(0)
if(IsControlPressed(0, 27) == false) then
listOn = false
SendNUIMessage({
meta = 'close'
})
break
end
end
end
end
end
end)
function GetPlayers()
local players = {}
for i = 0, 31 do
if NetworkIsPlayerActive(i) then
table.insert(players, i)
end
end
return players
end
function sanitize(txt)
local replacements = {
['&' ] = '&',
['<' ] = '<',
['>' ] = '>',
['\n'] = '<br/>'
}
return txt
:gsub('[&<>\n]', replacements)
:gsub(' +', function(s) return ' '..(' '):rep(#s-1) end)
end
I consider this a hack really, you should explore using a database for persistence and have it save all the character data.