Help with some lua code

im receving this error error parsing script @esx_zombiesystem/server/main.lua in resource esx_zombiesystem: @esx_zombiesystem/server/main.lua:35: 'then' expected near '='

code line 35 = if xPlayer ~= nil then

RegisterServerEvent('esx_zombiesystem:itemloot')
AddEventHandler('esx_zombiesystem:itemloot', function(item)
    local xPlayer = ESX.GetPlayerFromId(source)
	local random = math.random(1, 3)
    if xPlayer ~= nil then
        xPlayer.canCarryItem(item, random) then
        xPlayer.addInventoryItem(item, random)
        TriggerClientEvent("esx:showNotification", xPlayer.source, ('Você achou ~y~' .. random .. 'x ~b~' .. item))
    else
        xPlayer.showNotification('Você não pode pegá-lo porque seu inventário está cheio!')
    end
end)

someone can help me?

There is a random then

but how i fix?

i have removed the then now the error are in

local xPlayer = ESX.GetPlayerFromId(source)

Looks like you removed the if statement. Thats why there was a random “then” .Take a look at the original code to see what it suppose to like like

Replace that with this, as you can see it’s much easier to read and better handling expected output.

RegisterServerEvent('esx_zombiesystem:itemloot')
AddEventHandler('esx_zombiesystem:itemloot', function(item)
  local xPlayer = ESX.GetPlayerFromId(source)
  local random = math.random(1, 3)
  
  if xPlayer == nil or not next(xPlayer) then
    TriggerClientEvent('esx:showNotification', source, "~r~An error ocurred.");
    return
  end

  if not xPlayer.canCarryItem(item, random) then
    TriggerClientEvent('esx:showNotification', source, "~r~You can not carry that much.");
    return
  end

  xPlayer.addInventoryItem(item, random)
  TriggerClientEvent("esx:showNotification", source, ('Você achou ~y~' .. random .. 'x ~b~' .. item))

end)

If you get an error in the next part use this.

RegisterServerEvent('esx_zombiesystem:itemloot')
AddEventHandler('esx_zombiesystem:itemloot', function(item)
  local xPlayer = ESX.GetPlayerFromId(source)
  local random = math.random(1, 3)
  
  if xPlayer == nil then
    TriggerClientEvent('esx:showNotification', source, "~r~An error ocurred.");
    return
  end

  if not xPlayer.canCarryItem(item, random) then
    TriggerClientEvent('esx:showNotification', source, "~r~You can not carry that much.");
    return
  end

  xPlayer.addInventoryItem(item, random)
  TriggerClientEvent("esx:showNotification", source, ('Você achou ~y~' .. random .. 'x ~b~' .. item))

end)

now the error is here

SCRIPT ERROR: @esx_zombiesystem/server/main.lua:33: attempt to index a nil value (global 'ESX')

Do you have ESX setup?
Not sure if allowed to share this link, but take a look at this: GitHub - esx-framework/esx-legacy: Official Repo For ESX-Legacy if you haven’t already

yes i have ESX setup

ive fixed with this code, I had to complete the end of the code