How would I use ConextActionService for toggling a frame?

Hi!

Recently, I learned how to use the UserInputService.
But, that’s not compatible with Mobile.
The docs are very confusing on the ContextActionService, how would I use to to toggle a frame?

Thanks!

1 Like
local ContextActionService = game:GetService("ContextActionService")

local TOGGLE_FRAME_ACTION_NAME = "ToggleFrame"
local TOGGLE_FRAME_INPUT_TYPE = Enum.Keycode.F -- just an example

local frame = -- the frame

local function toggleFrame()
    frame.Visible = not frame.Visible
end

local function handleAction(actionName, inputState, inputObject)
    if actionName == TOGGLE_FRAME_ACTION_NAME and inputState == Enum.UserInputState.End then
        toggleFrame()
    end
end

ContextActionService:BindAction(TOGGLE_FRAME_ACTION_NAME, handleAction, true, TOGGLE_FRAME_INPUT_TYPE)
2 Likes

1 Like

I forgot that the enum is UserInputState, not InputState. I’ve fixed that now.

1 Like