We use cookies on this site to enhance your user experience

ContextActionService

ContextActionService

The ContextActionService is a game service that allows a game to bind user input to contextual actions, or actions that are only enabled under some condition or period of time. For example, allowing a player to open a door only while close by. In code, an action is simply a string (the name of the action) used by the service to differentiate between unique actions. The string is provided to BindAction and UnbindAction, among other member functions. If two actions are bound to the same input, the most recent takes priority. When the most recent action is unbound, the one bound before that takes control again.

You can inspect the currently bound actions in the developer console under the “Action Bindings” tab. You’ll also see bindings from the Roblox engine too. This is useful in checking if your actions are binding/unbinding at the correct times, or if some other action is stealing input from your actions. For exmaple, if you are attempting to bind WASD, it may be the case that character movement scripts is binding over those same keys.

ContextActionService is especially useful for supporting keyboard-less players, in which on-screen touch buttons can be used in place of key presses. The buttons will display only when the action is bound, and the position, text and/or images of these buttons can be configured through this service.

It’s better to use ContextActionService’s BindAction than UserInputService’s InputBegan for most cases. For example, if you want to use the R key to reload a weapon while it is equipped, the player might type “roblox is fun” in chat or otherwise use the R key for something else. The weapon could reload when the player didn’t mean to! If you instead use BindAction and UnbindAction when the weapon is equipped/unequipped, ContextActionService will make sure that R key presses trigger the reload action only when it is the most recently bound action.

Functions

Name Type Description
void

Bind user input to an action given an action handling function.

void

Binds function to fire when specified inputTypes occur. Allows the priority of the bound action to be specified.

void

Specifies the Enum/KeyCode that can be used with a Enum/UserInputType to activate a Tool, or a HopperBin.

Dictionary

Returns a table with all bound action info.

Dictionary

Returns a table with info regarding the function bound with actionName.

Instance

If actionName key contains a bound action, then this will return the touch button (if was created). Returns nil if a touch button was not created.

string

Returns the BackpackItem/TextureId of a tool currently equipped by the Player, if one is equipped.

void

If actionName key contains a bound action, then description is set as the description of the bound action.

void

If actionName key contains a bound action, then image is set as the image of the touch button.

void

If actionName key contains a bound action, then position is set as the position of the touch button.

void

If actionName key contains a bound action, then title is set as the title of the touch button.

void

Unbind an action from input given its name.

void

Undos the specification of a Enum/KeyCode that can be used with a Enum/UserInputType to activate a Tool, or a HopperBin, if it was set earlier using ContextActionService/BindActivate.

void

Removes all functions bound. No actionNames will remain. All touch buttons will be removed.

Inherited from Instance

ClearAllChildren Clone Destroy FindFirstAncestor FindFirstAncestorOfClass FindFirstAncestorWhichIsA FindFirstChild FindFirstChildOfClass FindFirstChildWhichIsA GetChildren GetDebugId GetDescendants GetFullName GetPropertyChangedSignal IsA IsAncestorOf IsDescendantOf WaitForChild

Events

Name Type Description

Fires when the current player equips a Tool.

Fires when the current player unequips a Tool.

Inherited from Instance

AncestryChanged Changed ChildAdded ChildRemoved DescendantAdded DescendantRemoving