We use cookies on this site to enhance your user experience

Creating Mobile Buttons

Creating Mobile Buttons

10 min

When designing an optimal cross-platform experience on Roblox, ContextActionService is a convenient service which lets you bind a function to traditional PC input and simultaneously create an on-screen button visible only on mobile devices. It also allows for quick manipulation of what input is tied to which functions, as well as when mobile action buttons are shown or not.

Adding Mobile Buttons

After ContextActionService has been declared in a LocalScript, the ContextActionService/BindAction|BindAction() method can be used to associate specific input with a function. This method takes the following parameters:

Parameter Type Description
actionName string A "key" used by other functions in ContextActionService to manipulate the binding.
functionToBind function The function to call when the specified input is triggered. This function will receive three arguments:
  • A string equal to the action name.
  • A enum/UserInputState|UserInputState which defines the input state when it called the function.
  • The InputObject that caused the function call.
createTouchButton boolean Whether or not you want an on-screen button to be created when the game is running on a mobile device.
inputTypes tuple The inputs you want to bind to the function, for instance enum values from enum/KeyCode|KeyCode.

Removing Mobile Buttons

If you want to remove a mobile button from the screen, call ContextActionService/UnbindAction|UnbindAction() with the sole argument of the actionName string you passed to ContextActionService/BindAction|BindAction().

Customizing Buttons

ContextActionService provides several functions to customize the buttons that are generated by ContextActionService/BindAction|BindAction().

Button Text

To change the text label for a mobile button, call ContextActionService/SetTitle|SetTitle() with the actionName string and a title:

Button Image

Mobile buttons can use custom images just like other GUI buttons via the ContextActionService/SetImage|SetImage() method:

Button Position

To position a mobile button, use ContextActionService/SetPosition|SetPosition() and specify the desired datatype/UDim2:

Overriding Inputs

Because screen space on mobile devices is very limited, it’s best practice to use contextual buttons that perform different actions based on what the player can or can’t do. For instance, you may want to display an active “Collect” button when the player is standing near a chest of gold:

At another point in the game, you may wish to change the button to “Talk” when the player is standing near an NPC. Instead of adding a new button in the same place (and removing the old button), you can simply re-bind its action to a new function and change its image:

  • mobile
  • controls
  • button