Bind game actions to keys with ContextActionService so the same code works on keyboard, touch, and gamepad.
In the last lesson you listened for specific keys. That works — until a on a phone tries to jump and finds there's no Space bar. ContextActionService fixes this: you bind a game action (Jump, Run, Dance) and Roblox maps it to whatever input that 's device can produce.
This code works on desktop, and nowhere else:
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.Space then
print("jump!")
end
end)
A phone has no keyboard. A gamepad has no Space bar. If you want the same action to work across devices, you don't check KeyCode for each device — you hand the mapping job to ContextActionService.
ContextActionService is also client-only. It belongs in a LocalScript, just like UserInputService.
ContextActionService:BindAction tells the engine: when this input happens, call my .
local ContextActionService = game:GetService("ContextActionService")
local function onJump(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("jump!")
end
end
ContextActionService:BindAction("jump", onJump, true, Enum.KeyCode.Space, Enum.UserInputType.Touch)
The arguments, in order:
"jump"), not the key.true to also create an on-screen button on mobile.Enum.KeyCode.Space and/or Enum.UserInputType.Touch. The same action, any number of inputs.What is the action name for?
Your handler gets called multiple times per press, not once. inputState tells you which :
Begin — the input started (key went down, finger touched). Fire the action here.End — the input stopped (key released). For jumping, usually do nothing; for holding-to-charge, start here.local function onJump(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
print("jump!")
end
end
Guard every handler with if inputState == Enum.UserInputState.Begin then. It's the same idea as the gameProcessedEvent guard — don't run the action twice.
The handler's first argument is the — a string naming the game action, not a specific key.
Bindings are scoped to a context. When the sits in a vehicle, you bind "honk". When they stand up, that binding must go away.
ContextActionService:UnbindAction("honk")
Or clear them all at once with ContextActionService:UnbindAllActions().
Forget to unbind and the old action keeps firing forever — you'll have stale bindings stacking up and buttons still on screen. Unbind when the context ends.
Bind the action 'sprint' to the LeftShift key. Create a touch button too, and print 'sprint!' only when the input begins.
local ContextActionService = game:GetService("ContextActionService")
-- define onSprint and bind it here
BindAction("Space", ...) breaks the whole point — name it after the game action so the engine can map devices.KeyCode inside the handler. You already told BindAction which inputs to watch. Let it do the mapping.Begin guard. The handler fires on Begin and End; without the guard your action runs twice.Next: ProximityPrompt — the modern walk-up-and-interact system.