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

FiveM vRP脚本执行staff命令遇InvokeNative参数为空错误求助

排查vRP服务器打印在线管理员列表的Lua代码错误

我尝试在服务器上打印所有在线管理员的员工列表,编写了如下Lua代码:

RegisterCommand('staff', function(source)
    local catistaff = 0
    local staff = vRP.getOnlineAdmins{};
    for k,v in pairs(staff) do
        local player = vRP.getUserSource{v}
        local name = GetPlayerName(player) or "Unknown"
        if name ~= "Unknown" then
            TriggerClientEvent('chatMessage', player, '^5' .. name .. ' ^0(^5' .. v .. '^0) -> ^1' .. vRP.getUserAdminTitle{v})
            catistaff = catistaff +1
        end
    end
    TriggerClientEvent('chatMessage', source, 'Staff Online: ^1' .. catistaff)
end)

执行后出现错误日志:

[    c-scripting-core] InvokeNative: execution failed: Argument at index 1 was null.
[          script:vrp] SCRIPT ERROR: Execution of native 000000002f7a49e6 in script host failed: Argument at index 1 was null.
[          script:vrp] > [global chunk] (TriggerClientEventInternal.lua:3)
[          script:vrp] > sendStaffMessage (@vrp/modules/admin.lua:411)
[          script:vrp] > callback (@vrp/modules/admin.lua:317)
[          script:vrp] > handler (vrp-lib/Tunnel.lua:139)
[          script:vrp] null

问题分析

  1. API调用语法错误:Lua中func{}等价于传入空表作为参数,但vRP的getOnlineAdmins、getUserSource、getUserAdminTitle这类API,要么无需参数,要么需要传入单个用户ID。错误的传参方式导致返回值为nil,进而让GetPlayerName、TriggerClientEvent接收到空参数,触发native调用失败。
  2. 未校验玩家有效性:即使修正传参,也可能存在管理员记录在线但实际已离线的情况,此时vRP.getUserSource会返回nil,直接传入native函数会报错。
  3. 逻辑不符合需求:原代码是给每个管理员发送自身信息,而非给执行staff命令的玩家展示所有管理员列表。

修复后的代码

RegisterCommand('staff', function(source)
    local catistaff = 0
    -- 修正无参数API的调用语法
    local staff = vRP.getOnlineAdmins()
    for _, user_id in pairs(staff) do
        -- 用正确格式传递用户ID参数
        local player = vRP.getUserSource(user_id)
        -- 先校验玩家源是否有效,避免传入nil
        if player then
            local name = GetPlayerName(player) or "Unknown"
            local admin_title = vRP.getUserAdminTitle(user_id) or "No Title"
            -- 给执行命令的玩家发送管理员信息
            TriggerClientEvent('chatMessage', source, '^5' .. name .. ' ^0(^5' .. user_id .. '^0) -> ^1' .. admin_title)
            catistaff = catistaff + 1
        end
    end
    TriggerClientEvent('chatMessage', source, 'Staff Online: ^1' .. catistaff)
end)

关键修复点

  • 将所有func{}的错误调用格式,改为符合vRP API规范的func(param)传参方式。
  • 增加if player then的有效性校验,确保只有合法玩家源才会调用native函数。
  • 调整TriggerClientEvent的目标为执行命令的source,实现“给调用者展示管理员列表”的核心需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 18:05:15