Opening Roblox Menu triggers ContextActionService

I have a randomly generating difficulty chart obby with the general “Reset Button”.

I linked this to ContextActionService with Enum.KeyCode.R.
None of my scripts whatsoever mention the Roblox Menu yet when I open it, it triggers the reset functionality.

This is the handler that manages the Reset Button GUI:

local button = script.Parent.Button
button.Visible = game:GetService("UserInputService").TouchEnabled

local player = game.Players.LocalPlayer
local character = player,Character or player.CharacterAdded:Wait()

local function reset()
    if not player:GetAttribute("Menu") then --note that menu here means the game's MainMenu
        character:PivotTo(CFrame.new(0, 3, 0) * if player:GetAttribute("Stage") == 0 then workspace.Core.Spawn.Spawn:GetPivot() else workspace.Core.Stages[tostring(player:GetAttribute("Stage"))].Checkpoint:GetPivot())
    end
    character:FindFirstChildOfClass("Humanoid").Health = character:FindFirstChildOfClass("Humanoid").MaxHealth
end

local i = 0
game:GetService("ContextActionService"):BindAction("ResetPlayer", function()
    i += 1
    if i % 2 == 1 then
        reset()
    end
end, false, Enum.KeyCode.R)
button.Activated:Connect(reset)

Video of issue:

Binding Enum.KeyCode.R with ContextActionService, will also fire when clicking the Roblox menu - Bug Reports / Engine Bugs - Developer Forum | Roblox

When the settings menu is opened, the R key is bound to the “Reset” key in the menu. This triggers a ContextAction with the InputState of "Cancel’ (Enum.UserInputState.Cancel). Roblox does this so that if your function had to reset anything when it is unbound / superseded by another ContextAction, it is able to do so.

You can fix this by making sure your function only fires when the InputState is equal to Enum.UserInputState.Begin, or in other words, when the player starts pressing down the R key.

local i = 0
game:GetService("ContextActionService"):BindAction("ResetPlayer", 
	function(actionName, inputState, inputObject)
		if inputState ~= Enum.UserInputState.Begin then return end
		i += 1
		if i % 2 == 1 then
			reset()
		end
	end, false, Enum.KeyCode.R)

Further readings:
ContextActionService:BindAction() | Documentation - Roblox Creator Hub
UserInputState | Documentation - Roblox Creator Hub

1 Like

Thank you! This worked very well.

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