Use ESX character name instead of Steam name in chat

I’ve just recently started working on a new small project to learn more about lua and fivem resource development. I can’t figure out how I change the chat name to use the current ESX character instead of regular Steam name. The code I use for both server.lua and client.lua is pasted bellow.

Client.lua

RegisterNetEvent('sendLokalOOC')
AddEventHandler('sendLokalOOC', function(id, name, message)
  local myId = PlayerId()
  local pid = GetPlayerFromServerId(id)
  if pid == myId then
    TriggerEvent('chatMessage', "LOKAL OOC | ", {0, 177, 177}, " ^7 " .. name .." ".."^7 " .. message)
  elseif GetDistanceBetweenCoords(GetEntityCoords(GetPlayerPed(myId)), GetEntityCoords(GetPlayerPed(pid)), true) < 19.999 then
    TriggerEvent('chatMessage', "LOKAL OOC | ", {0, 177, 177}, " ^7 " .. name .." ".."^7 " .. message)
  end
end)

Server.lua

-- LOKAL OOC --
RegisterCommand('lokalooc', function(source, args, user)
    local name = GetPlayerName(source)
    TriggerClientEvent("sendLokalOOC", -1, source, name, table.concat(args, " "))
end, false)

-- TWITTER --
RegisterCommand("twitter", function(source, args, raw)
    local _source = source
    local name = GetPlayerName(_source)
    local message = table.concat(args, " ")
    TriggerClientEvent('chatMessage', -1, "TWITTER | " .. name, { 30, 144, 255 }, message)
end, false)

RegisterCommand("twt", function(source, args, raw)
    local _source = source
    local name = GetPlayerName(_source)
    local message = table.concat(args, " ")
    TriggerClientEvent('chatMessage', -1, "TWITTER | " .. name, { 30, 144, 255 }, message)
end, false)

-- BLOCKET --
RegisterCommand("blocket", function(source, args, raw)
    local _source = source
    local name = GetPlayerName(_source)
    local message = table.concat(args, " ")
    TriggerClientEvent('chatMessage', -1, "BLOCKET | " .. name, { 255, 0, 0 }, message)
end, false)

-- ENCRO --
RegisterCommand("encro", function(source, args, raw)
    local _source = source
    local message = table.concat(args, " ")
    TriggerClientEvent('chatMessage', -1, "ENCROCHAT | @Anonym", { 136, 59, 204 }, message)
end, false)

-- RÖR EJ --
function stringsplit(inputstr, sep)
  if sep == nil then
      sep = "%s"
  end
  local t={} ; i=1
  for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
      t[i] = str
      i = i + 1
  end
  return t
end

Btw this is supposed to be a swedish translated script if your wondering, that’s why there is a “blocket” server command.

So to be clear I want to change local name = GetPlayerName(_source) to get the CURRENT ESX CHARACTER name instead of steam.

1.) You need to get the ESX object in the resource you want to use it in. ESX has thorough explanations describing how to do this
2.) You need to get the player object from ESX using the source ID. ESX scripts contain many references showing how to do this, usually referred to as xPlayer
3.) You need to use the given .get(‘firstname’) and .get(‘lastname’) functions provided in the player object then concatenate those two values together with a space between.

1 Like