Show Menu

Roblox: Gamepad Input Cheat Sheet by

A quick reference for implementing gamepad (controller) input on Roblox. This cheatsheet is written for intermediate scripters.

Contex­tAc­tio­nSe­rvice basics

Used for binding to inputs gracef­ully. If an input is bound to one action and another action is to use the same button, the BindAction and UnbindAction functions will handle the collision properly. Example: (A) could make the player jump OR open a door but only when close enough.
Each input type bound to a function using BindAction works like a stack: whichever function was the most recent to be bound will be called when that input type is activated by the player.

Contex­tAc­tio­nSe­rvice binding

:BindAction(name, func, touchButton, inputTypes...)
Calls func with name, InputS­tate, and InputO­bject when input­Types are used.
Unbinds a function from action name.
Input types for :BindAction():
As soon as a player can use an input (like A/B, triggers, or thumbs­tick), use :Bind­Act­ion­(...). This will override the given input types current actions with the new one. When the player can no longer use the input for the action, use :Unbi­ndA­cti­on(­name).

Contex­tAc­tio­nSe­rvice example

local cas = game:GetService("ContextActionService")
function handleAction(actionName, inputState, inputObject)
   if actionName == "Swing sword" and inputState == Enum.UserInputState.Begin then
      print("Swinging sword")

-- When the sword is equipped:
cas:BindAction("Swing sword", handleAction, false, Enum.KeyCode.ButtonA)
-- When the sword is unequipped:
cas:UnbindAction("Swing sword")
When the given input type is activa­ted­/ch­anged, the function passed to :BindA­cti­on(...) is called with the action name, the input state (Beg­in, Chan­ge, End or Canc­el) and the InputObject. It's good practice to have just one action­-ha­ndling function per script.

Wiki References


Gamepad KeyCodes and UserIn­put­Types

Buttons use the UserIn­put­States Began/End. Triggers and thumbs­ticks use Change.

UserIn­put­Service querying

Property (boolean): GamepadEnabled
Returns true if at least one gamepad is connected.
Event: GamepadConnected
Fired when a gamepad is available.
Fired when a gamepad is no longer available.
Avoid using UserI­npu­tSe­rvi­ce.I­np­utB­egan for actions involving button presses! Use Conte­xtA­cti­onS­erv­ice­:Bi­ndA­cti­on(...) because this "­ove­rri­des­" existing actions using the given input types, and properly returns control to existing actions when yours are unbound.

Tips and Good Practices

A Button - Enum.K­eyC­ode.Bu­ttonA
Bound to jump by default. Should be used as accept button for prompts.
B Button - Enum.K­eyC­ode.Bu­ttonB
Should be used as back or cancel button for menus/­pro­mpts.
Right Trigger - Enum.K­eyC­ode.Bu­ttonR2
Use for prim­ary character actions.
Left Trigger - Enum.K­eyC­ode.Bu­ttonL2
Use for seco­ndary character actions.
Right Thumbs­tick - Enum.K­eyC­ode.Th­umb­stick1
Use for camera movement.
Left Thumbs­tick - Enum.K­eyC­ode.Th­umb­stick2
Use for char­acter movement.
Righ­t/Left Bumpers - Enum.K­eyC­ode.Bu­tto­nR1­/Bu­ttonL1
Bound to switch tools by default.
A good way to know what kinds of control schemes work is by playing other gamepa­d/c­ont­roller enabled games.

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Roblox: General Scripting Cheat Sheet
          Roblox: CFrames Cheat Sheet

          More Cheat Sheets by Ozzypig

          Roblox: General Scripting Cheat Sheet
          Roblox: CFrames Cheat Sheet