You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

搭建GTA服务器时mrp-gangs脚本出现xKiller空值索引错误如何解决

报错根因

错误触发的核心原因是未对ASD.GetPlayerFromId()的返回结果做空值校验:当传入的玩家ID无效时(比如对应玩家已经离线、ID为非法的0/负值、参数被篡改),方法会返回nil值,后续直接调用xKiller.getIdentifier()就会触发空索引异常。

修复方案
  • 对asd_gangs:sumPlayerKill事件添加入参校验、返回值校验,无效场景直接终止逻辑,避免运行报错
  • 可选优化:对asd:onPlayerDeath事件也加对应校验,避免给不存在的玩家推送客户端事件
修改后代码
RegisterServerEvent('asd:onPlayerDeath')
AddEventHandler('asd:onPlayerDeath', function(data)
    -- 先校验击杀者相关参数合法性
    if data.killedByPlayer and data.killerServerId and type(data.killerServerId) == 'number' and data.killerServerId > 0 then
        local xKiller = ASD.GetPlayerFromId(data.killerServerId)
        -- 仅当击杀者玩家在线时才推送事件
        if xKiller then
            TriggerClientEvent('asd_gangs:playerKilled', data.killerServerId)
        end
    end
end)
 
RegisterServerEvent('asd_gangs:sumPlayerKill')
AddEventHandler('asd_gangs:sumPlayerKill', function(id)
    -- 校验传入的玩家ID合法性
    if not id or type(id) ~= 'number' or id <= 0 then 
        -- 可选:添加日志排查恶意请求
        -- print(string.format("非法击杀统计请求,传入ID:%s,请求来源:%s", id, source))
        return 
    end
    local xKiller = ASD.GetPlayerFromId(id)
    -- 校验玩家对象存在再执行后续逻辑
    if not xKiller then return end
    MySQL.Sync.execute("UPDATE users SET kills = kills + 1 WHERE identifier = @identifier", {['@identifier'] = xKiller.getIdentifier()})
end)

内容的提问来源于stack exchange,提问作者Bobo Jackson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 00:48:03