Binding Enum.KeyCode.R with ContextActionService, will also fire when clicking the Roblox menu

I recently was using ContextActionService to bind a couple keyboard inputs to functions when I noticed that clicking the menu button in the top left corner would fire the function related to the action connected to Enum.KeyCode.R It fires the function three times and only when opening the menu, not when closing it

I was successfully able to replicate this in another game where I just had a function triggered by connecting R to ContextActionService. This happens 100% of the time. You can replicate this yourself by running the following code in the client and pressing either R or clicking the menu, even though only R should fire the function:

local ContextActionService = game:GetService("ContextActionService")

local function pressedR()
	print("Fired!")
end

ContextActionService:BindAction("Fire Function", pressedR, false, Enum.KeyCode.R)

This occurs in vanilla Studio with no plugins or gametests enabled to get weird behavior. I am in the beta test program, but all beta features were turned off while testing this to ensure they were not the problem.

1 Like

This is expected behaviour, you need to check the input state passed to the function. When the R button is bound over by the in game menu, your function is fired with the Cancel UserInputState. This is so you can reset any state that needs to be reset when your bound action is superseded by another action.

You can fix this by only running the function on Enum.UserInputState.Begin

local ContextActionService = game:GetService("ContextActionService")

local function pressedR(actionName, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		print("Fired!")
	end
end

ContextActionService:BindAction("Fire Function", pressedR, false, Enum.KeyCode.R)
11 Likes

I apologize, I did not see this documented anywhere so I assumed it was a bug. Thanks for letting me know!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.