使用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
相关产品推荐
相关产品推荐

