FiveM服务器,表索引为 nil && 试图索引空值 (local, xPlayer)

我为我的服务器添加了一些插件,但一直出现这两个错误。

这是我的两个代码。

1:表索引为 nil,

ESX.RegisterServerCallback('esx_weashop:requestDBItems', function(source, cb)

  MySQL.Async.fetchAll(
    'SELECT * FROM weashops',
    {},
    function(result)

      local shopItems  = {}

      for i=1, #result, 1 do

        if shopItems[result[i].name] == nil then
          shopItems[result[i].name] = {}
        end

        table.insert(shopItems[result[i].name], {
          name  = result[i].item,
          price = result[i].price,
          label = ESX.GetWeaponLabel(result[i].item)
        })
      end

      if Config.EnableClipGunShop == true then
        table.insert(shopItems["GunShop"], {
          name  = "clip",
          price = GunShopPrice,--Config.EnableClip.GunShop.Price,
          label = GunShopLabel--Config.EnableClip.GunShop.label
        })
        end

        if Config.EnableClipGunShop == true then
        table.insert(shopItems["BlackWeashop"], {
          name  = "clip",
          price = BlackWeashopPrice,--Config.EnableClip.BlackWeashop.Price,
          label = BlackWeashopLabel--Config.EnableClip.BlackWeashop.label
        })
        end
      cb(shopItems)

    end
  )

end)

2:试图索引空值 (local, xPlayer)

AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
    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)
点赞
用户2858170
用户2858170

@1 表格索引为零

第一个错误告诉你正在使用空值作为表格索引。我无法告诉你是哪个,因为您未在错误消息中贴出带有行号的完整信息。

它是由类似于a = b[c]a = {[c] = b}的东西引起的,其中cnil

因此,在使用 shopItems 索引 result[i].nameresult[i].item 时,其中一个为 nil

@2 尝试对空值进行索引(本地,xPlayer)

这更加明显。xPlayer 为空,因此您不能像 xPlayer.identifier 一样对其进行索引。

因此,请确保 xPlayer 永远不为空,或者仅在可索引时才对其进行索引。

快速查看 esx手册 会显示

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(playerData)

end)

因此,根据手册,仅有一个参数。您有两个,当带有参数调用函数时,第二个参数为nil

2021-01-20 21:34:17