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

使用Roblox ContextActionService报错“Argument 2 missing or nil”求助

问题解决:ContextActionService 报错 "Argument 2 missing or nil"

错误原因

你的代码调用BindAction时,第二个参数传的是onButtonPress()——这会直接执行该函数,而onButtonPress没有返回值,导致实际传入的参数是nil,这就是报错的根源。BindAction需要的是函数引用,而非函数执行后的结果。

修正后的代码

local ContextActionService = game:GetService("ContextActionService")
local Part = game.Workspace.Part

local function onButtonPress()
    Part.BrickColor = BrickColor.new("New Yeller")
end

-- 移除函数后的括号,传入函数本身而非执行结果
ContextActionService:BindAction("turnbrickyellow", onButtonPress, true, Enum.KeyCode.T)

补充说明

  • 第三个参数设为true,Roblox会自动在移动设备(如手机)上生成可点击的按钮,对应绑定的Enum.KeyCode.T操作,满足你在手机端生成按钮的需求。
  • 确保Part确实存在于Workspace中,且LocalScript的位置正确(比如放在StarterPlayerScripts或StarterPack里,客户端才能正确访问)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 19:32:01