Show Menu
Cheatography

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 Unbind­Action 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
inputTypes
are used.
:UnbindAction(name)
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
:BindA­cti­on(...)
. 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
:Unbin­dAc­tio­n(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")
   end
end

-- 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 (Begin, Change, End or Cancel) and the InputO­bject. 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.
Event:
Gamepa­dDi­sco­nnected
Fired when a gamepad is no longer available.
Avoid using
UserIn­put­Ser­vic­e.I­npu­tBegan
for actions involving button presses!
Use
Contex­tAc­tio­nSe­rvi­ce:­Bin­dAc­tio­n(...)
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 primary character actions.
Left Trigger - Enum.K­eyC­ode.Bu­ttonL2
Use for secondary character actions.
Right Thumbstick - Enum.K­eyC­ode.Th­umb­stick1
Use for camera movement.
Left Thumbstick - Enum.K­eyC­ode.Th­umb­stick2
Use for character movement.
Right/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.
                   
 

Comments

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