I have some problem with server and client event, help!

first, sorry for my poor english, im not native in english. I want to create a plugin like devided players into groups without esx, first todo is to get the players identifier, such license: or steam: etc. and I have problem on getting it.
function GetPlayerIdentifier is a server function. so I should let client send request to server and server return it.
my code is like this
client.lua

--获取玩家ped,id,用于和服务端通信
--get player ped to communicate with server
local ped = GetPlayerPed(-1)
local id = GetPlayerServerId(-1);
print('ped' .. ped.. 'id'..id)

--要求服务端传回玩家标识符
--ask server for player identifiers
RegisterCommand('identifier', function()
TriggerServerEvent('getplayeridentifier', id )
end,false)

--接受服务端传回的标识符
--receive the identifier from server
RegisterNetEvent('receiveidentifier')
AddEventHandler('receiveidentifier', function (license)
     license = license;
    print('license'..license)
end)

server.lua

local steamid  = false
local license  = false
local discord  = false
local xbl      = false
local liveid   = false
local ip       = false
--接受客户端标识符的命令,产生标识符
--receive the command from the client and get identifiers
RegisterServerEvent('getplayeridentifier');
AddEventHandler('getplayeridentifier', function(id)
id = id;
  for k,v in pairs(GetPlayerIdentifiers(source))do

    
    if string.sub(v, 1, string.len("steam:")) == "steam:" then
    steamid = v
    elseif string.sub(v, 1, string.len("license:")) == "license:" then
    license = v
    elseif string.sub(v, 1, string.len("xbl:")) == "xbl:" then
    xbl  = v
    elseif string.sub(v, 1, string.len("ip:")) == "ip:" then
    ip = v
    elseif string.sub(v, 1, string.len("discord:")) == "discord:" then
    discord = v
     elseif string.sub(v, 1, string.len("live:")) == "live:" then
     liveid = v
     end
   end

  --将玩家标识符传回客户端
  --send the player identifiers to the client
  TriggerClientEvent('receiveidentifier', id, license)
end)

and when I ensure this plugin and type command /identifier, client do not return anything.
and when I add print() on the server.lua, the console do not return anything either.
I do not know what the problem is, I am looking for help!

So the first issue is how you attempt to collect the license, instead of using string.sub you should use string.find() to check if the string contains the word you are looking for. In your case this could look something like this:

string.find(v, 'license:')

Now, that returns something like this “license:xxx”, the x’s being a bunch of “random” letters and numbers. I presume you want the ‘license:’ part removed, to do that you would use (in your case) v:gsub(‘license:’, “”), this will find ‘license:’ and replace it with an empty string.

In the example above it would look like this:

elseif string.find(v, 'license:') then
    license = v:gsub('license:', "")

The second problem is how you collect the player server id on the client, GetPlayerServerId needs the player id to return the server id, -1 will not return this client’s server id (unlike GetPlayerPed). You will have to use PlayerId to get the player id. In your case this would work:

local id = GetPlayerServerId(PlayerId())

You can read more about pattern matching functions in lua here.

Best of luck with your coding endeavours!

A completely unrelated tip is to replace how you are fetching the player’s Ped object. The more optimized method is like so:

local ped = PlayerPedId()

Why this is so and more info can be found on this helpful post here: [Question] Difference between GetPlayerPed(-1) and PlayerPedId()

Goodluck! :slight_smile:

thank a lot for your reply and share.but I have decided to add essentialmode (es_extended) and use the job system to divide players. I think functions provided by ESX will be more easy to do that. if I have same problems in lua in the future, I will look back at this post. thank again for your share!!!

1 Like