ESX.GetPlayers() returns a list of player ids that have a character loaded.

Resources that use this function then loop through the IDs and use ESX.GetPlayerFromId(id), then perform a whatever task they want with that without any sort of wait. Sometimes this is an async query - so you send 200 database queries almost all at once, or some events (which are in themselves async).

You can alleviate some of the stress by changing how the xPlayer data is retrieved.

Add the following function to ESX/server/functions.lua

ESX.GetExtendedPlayers = function()
	return ESX.Players
end

Example of a typical xPlayer loop

		local xPlayers = ESX.GetPlayers()
		for _,playerId in ipairs(xPlayers) do
			local xPlayer = ESX.GetPlayerFromId(playerId)

			MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
				['@identifier'] = xPlayer.identifier
			}, function(result)
				local data = {}

				if result[1].status then
					data = json.decode(result[1].status)
				end

				xPlayer.set('status', data)
				TriggerClientEvent('esx_status:load', playerId, data)
			end)
		end

Replacement

		local xPlayers = ESX.GetExtendedPlayers()
		for k,v in pairs(xPlayers) do		
			MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
				['@identifier'] = v.identifier
			}, function(result)
				local data = {}

				if result[1].status then
					data = json.decode(result[1].status)
				end

				v.set('status', data)
				TriggerClientEvent('esx_status:load', k, data)
			end)
		end

If you are using any resources for blips (for EMS, police, etc) check how they are performing this task as well. By default ambulancejob and policejob send a server event when a cop loads in, which tells all clients to check if they are ems/police and send a server callback.

The callback, which is triggered by (for example) 6 cops then runs 6 async xPlayer loops to get the ids of all active cops and report back to the client.

Lastly, if you are using gcphone then you need to replace the following function

function getSourceFromIdentifier(identifier, cb)
	local xPlayers = ESX.GetPlayers()
	
	for i=1, #xPlayers, 1 do
		local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
		
		if(xPlayer.identifier ~= nil and xPlayer.identifier == identifier) or (xPlayer.identifier == identifier) then
			cb(xPlayer.source)
			return
		end
	end
	cb(nil)
end

Replace with

function getSourceFromIdentifier(identifier, cb)
	local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
	if xPlayer then cb(xPlayer.source) else cb(nil) end
end
5 Likes