[Help] TriggerServerCallback

so i try to get player information from

ESX.RegisterServerCallback('esx_policejob:getOtherPlayerData', function(source, cb, target)

in esx_policejob

This my is code. it trigger from client ESX.TriggerServerCallback('esx_policejob:getOtherPlayerData', function(data) table.insert(playerWithNames, {id = GetPlayerServerId(i), firstname = data.firstname, lastname = data.lastname})
it work fine but alway get this error

Error running call reference function for resource esx_policejob: citizen:/scripting/lua/scheduler.lua:405: @esx_policejob/server/main.lua:220: attempt to index a nil value (local 'xPlayer')
stack traceback:

any solution for it ?

any help pls!

1 Like

The error may not be related to what you have pasted above. What’s on line 220 of esx_policejob/server/main.lua ?

ESX.RegisterServerCallback('esx_policejob:getOtherPlayerData', function(source, cb, target)
    local xPlayer = ESX.GetPlayerFromId(target)
	local identifier = GetPlayerIdentifiers(target)[1]
	if Config.EnableESXIdentity and xPlayer ~= nil then
		
		
		local result = MySQL.Sync.fetchAll('SELECT firstname, lastname, sex, dateofbirth, height FROM users WHERE identifier = @identifier', {
			['@identifier'] = identifier
		})

		local firstname = result[1].firstname
		local lastname  = result[1].lastname
		local sex       = result[1].sex
		local dob       = result[1].dateofbirth
		local height    = result[1].height

		local data = {
			name      = GetPlayerName(target),
			job       = xPlayer.job,
			inventory = xPlayer.inventory,
			accounts  = xPlayer.accounts,
			weapons   = xPlayer.loadout,
			firstname = firstname,
			lastname  = lastname,
			sex       = sex,
			dob       = dob,
			height    = height
		}

		TriggerEvent('esx_status:getStatus', target, 'drunk', function(status)
			if status ~= nil then
				data.drunk = math.floor(status.percent)
			end
		end)

		if Config.EnableLicenses then
			TriggerEvent('esx_license:getLicenses', target, function(licenses)
				data.licenses = licenses
				cb(data)
			end)
		else
			cb(data)
		end
	else
		local xPlayer = ESX.GetPlayerFromId(target)

		local data = {
			name       = GetPlayerName(target),
			job        = xPlayer.job,
			inventory  = xPlayer.inventory,
			accounts   = xPlayer.accounts,
			weapons    = xPlayer.loadout
		}

		TriggerEvent('esx_status:getStatus', target, 'drunk', function(status)
			if status then
				data.drunk = math.floor(status.percent)
			end
		end)

		TriggerEvent('esx_license:getLicenses', target, function(licenses)
			data.licenses = licenses
		end)

		cb(data)
	end
end)

this

im sure it came from my resource. but i dont know how to fix it. it still work but alway popup the error.

It would appear you are not passing the server the ‘target’ variable when you execute the TriggerServerCallback, so it’s failing to assign to xPlayer on the server side.

If this is what you have on the client:

ESX.TriggerServerCallback('esx_policejob:getOtherPlayerData', function(data) table.insert(playerWithNames, {id = GetPlayerServerId(i), firstname = data.firstname, lastname = data.lastname})

Instead try:

ESX.TriggerServerCallback('esx_policejob:getOtherPlayerData', function(data) 
     table.insert(playerWithNames, {id = GetPlayerServerId(i), firstname = data.firstname, lastname = data.lastname})
end,  GetPlayerServerId(i))

You might need to paste more of your client code in because I’m not really sure where your GetPlayerServerId(i) is coming from but I assume that is from some for i = 0, 32 loop checking if NetworkIsPlayerActive(i), but maybe I’m wrong.

Citizen.CreateThread(function()
  while ESX == nil do
    TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
    Citizen.Wait(0)
  end
 
    while true do
        Citizen.Wait(5000)
 
        for i = 0, 255 do
            if NetworkIsPlayerActive(i) then
                local iPed = GetPlayerPed(i)
                local lPed = PlayerPedId()
                local playerServerId = GetPlayerServerId(i)
                if iPed ~= lPed then
                    if DoesEntityExist(iPed) then
                        if not doesIdIsRegistred(playerServerId) then
                            ESX.TriggerServerCallback('esx_policejob:getOtherPlayerData', function(data)
                                table.insert(playerWithNames, {id = GetPlayerServerId(i), firstname = data.firstname, lastname = data.lastname})
                            end, GetPlayerServerId(i))
                        end
                    end
                end
            end
        end
    end
end)

already do it

What is on line 220 in server/main.lua then?

You can also try replacing the two GetPlayerServerId(i) with playerServerId variable since it’s already declared in scope, not sure that has anything to do with it or not.

 local playerServerId = playerServerId(i)
                if iPed ~= lPed then
                    if DoesEntityExist(iPed) then
                        if not doesIdIsRegistred(playerServerId) then
                            ESX.TriggerServerCallback('esx_policejob:getOtherPlayerData', function(data)
                                table.insert(playerWithNames, {id = playerServerId(i), firstname = data.firstname, lastname = data.lastname})
                            end, playerServerId(i))

right ??
which file? server/main.lua ?? exs_policejob ? i use custom head label resource… just try to edit the client side only

This file is where your error is coming from on line 220

Error running call reference function for resource esx_policejob/server/main.lua:220: attempt to index a nil value (local 'xPlayer')

Just wondering exactly what is on line 220 in that file since that is what you’re ultimately trying to solve for.

ESX.RegisterServerCallback('esx_policejob:getOtherPlayerData', function(source, cb, target)
    local xPlayer = ESX.GetPlayerFromId(target)
	local identifier = GetPlayerIdentifiers(target)[1]
	if Config.EnableESXIdentity and xPlayer ~= nil then
		
		
		local result = MySQL.Sync.fetchAll('SELECT firstname, lastname, sex, dateofbirth, height FROM users WHERE identifier = @identifier', {
			['@identifier'] = identifier
		})

		local firstname = result[1].firstname
		local lastname  = result[1].lastname
		local sex       = result[1].sex
		local dob       = result[1].dateofbirth
		local height    = result[1].height

		local data = {
			name      = GetPlayerName(target),
			job       = xPlayer.job,
			inventory = xPlayer.inventory,
			accounts  = xPlayer.accounts,
			weapons   = xPlayer.loadout,
			firstname = firstname,
			lastname  = lastname,
			sex       = sex,
			dob       = dob,
			height    = height
		}

		TriggerEvent('esx_status:getStatus', target, 'drunk', function(status)
			if status ~= nil then
				data.drunk = math.floor(status.percent)
			end
		end)

		if Config.EnableLicenses then
			TriggerEvent('esx_license:getLicenses', target, function(licenses)
				data.licenses = licenses
				cb(data)
			end)
		else
			cb(data)
		end
	else
		local xPlayer = ESX.GetPlayerFromId(target)

		local data = {
			name       = GetPlayerName(target),
			job        = xPlayer.job,
			inventory  = xPlayer.inventory,
			accounts   = xPlayer.accounts,
			weapons    = xPlayer.loadout
		}

		TriggerEvent('esx_status:getStatus', target, 'drunk', function(status)
			if status then
				data.drunk = math.floor(status.percent)
			end
		end)

		TriggerEvent('esx_license:getLicenses', target, function(licenses)
			data.licenses = licenses
		end)

		cb(data)
	end
end)

that it

You must have made some changes since posting that error because line 220 is empty in that picture, I’d expect that error on line 211 where you assign to xPlayer, or possibly 215-218 where you use it’s properties. If you get the error again paste it in here showing the new line number and also what’s on that line in the server file.

OK let me check with the change the GetPlayerServerId to playerServerId

if GetPlayerServerId(i) ~= nil then

code callback

end