Search code examples
luaroblox

ROBLOX ContextActionService giving me"Argument 2 missing or nil"


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)

I tried researching then realise I did nothing wrong, expect it to run and put a button somewhere to press for phones. BTW my script is localscript, so yea.


Solution

  • ContextActionService:BindAction takes a functionToBind, a function which is called each time the input is fired.

    In your code, you do ContextActionService:BindAction("turnbrickyellow", onButtonPress(), true, Enum.KeyCode.T) where you call onButtonPress() as a function (when you do this, the return value of that function is essentially nil)

    Instead, you would want to do

    ContextActionService:BindAction("turnbrickyellow", onButtonPress, true, Enum.KeyCode.T)