You need to enable JavaScript to run this app. FiveM ESX服务器table index is nil与xPlayer空值索引错误求助
优惠活动
大模型
产品
解决方案
定价
更多

FiveM ESX服务器table index is nil与xPlayer空值索引错误求助

报错修复方案

报错1:table index is nil(对应esx_weashop模块)

核心问题

  • 执行table.insert操作时,目标商店对应的表为nil:当数据库weashops表中不存在GunShop/BlackWeashop对应的商店条目时,shopItems表中这两个键不会被循环初始化,直接向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
        local shopName = result[i].name
        -- 补充校验,避免数据库条目name为nil触发错误
        if shopName then
          if shopItems[shopName] == nil then
            shopItems[shopName] = {}
          end

          table.insert(shopItems[shopName], {
            name  = result[i].item,
            price = result[i].price,
            label = ESX.GetWeaponLabel(result[i].item)
          })
        end
      end
      
      -- 枪店加弹药逻辑
      if Config.EnableClipGunShop == true then
        -- 提前初始化商店键,避免对nil表执行插入
        if shopItems["GunShop"] == nil then
          shopItems["GunShop"] = {}
        end
        table.insert(shopItems["GunShop"], {
          name  = "clip",
          price = GunShopPrice,
          label = GunShopLabel
        })
      end
      
      -- 黑店加弹药逻辑,修正判断条件为黑店独立开关(按实际配置项调整判断值)
      if Config.EnableClipBlackWeashop == true then
        if shopItems["BlackWeashop"] == nil then
          shopItems["BlackWeashop"] = {}
        end
        table.insert(shopItems["BlackWeashop"], {
          name  = "clip",
          price = BlackWeashopPrice,
          label = BlackWeashopLabel
        })
      end
      cb(shopItems)

    end
  )

end)

报错2:attempt to index a nil value (local, xPlayer)(对应玩家状态同步模块)

核心问题

  • 异步MySQL查询的回调执行时,外层作用域的xPlayer可能已经被回收:如果玩家在查询等待期间掉线,ESX会清理对应的xPlayer实例,此时引用该对象就会触发nil索引错误
  • 原代码未校验MySQL查询结果的有效性,当玩家无对应status记录时,result[1]为nil,会触发二次索引错误

修复要点

  • 在异步执行前预取必要的玩家标识符,避免依赖被回收的xPlayer对象
  • 异步回调内重新通过ESX原生方法获取当前在线玩家的有效xPlayer实例
  • 补充全链路空值判断,所有可能为nil的节点都增加校验

修复后代码

AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
    -- 异步执行前预取标识符,提前校验xPlayer有效性
    local identifier = xPlayer and xPlayer.identifier
    if not identifier then return end

    MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
        ['@identifier'] = identifier
    }, function(result)
        -- 异步回调内重新校验玩家在线状态,获取有效xPlayer实例
        local currentXPlayer = ESX.GetPlayerFromId(playerId)
        if not currentXPlayer then return end

        local data = {}
        -- 全链路校验查询结果有效性
        if result and result[1] and result[1].status then
            data = json.decode(result[1].status) or {}
        end

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

内容的提问来源于stack exchange,提问作者Jay van Veen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.08 10:15:07