Lesson 12 of 12
Input everywhere, part 2: move the yard to the Input Action System
Move Beacon Yard's Controls.luau from ContextActionService to the Input Action System, with a play context, a higher sinking menu context, Pressed events and touch buttons you draw.
By the end you can
- Build Beacon Yard's controls from an InputContext per context, an InputAction per control and an InputBinding per key
- Draw touch buttons and link them with UIButton, and keep a menu's keys out of gameplay with a higher, sinking context
- Move Controls.luau and CameraClient's zoom handler off ContextActionService without changing any other script
Before this: Input everywhere, part 1: keyboard, gamepad, touch and accessibility
1What you are replacing
At m14-ship, Controls.luau still holds the one table of bindings from part 1 and turns it into ContextActionService calls. This is the pair of functions you will rewrite:
-- Binds one action on keyboard, gamepad and touch at once. A second
-- bind with the same name replaces the first, as BindAction does.
function Controls.bind(name: ActionName, handler: Handler)
local binding = BINDINGS[name]
ContextActionService:BindAction(name, handler, binding.touchButton, table.unpack(binding.inputs))
if binding.touchButton then
ContextActionService:SetTitle(name, binding.title)
end
end
function Controls.unbind(name: ActionName)
ContextActionService:UnbindAction(name)
CombatClient, CameraClient and ShopGui never call BindAction themselves. They call Controls.bind with an action name and a handler, so the work sits in one file.
You move Controls.bind from BindAction to the Input Action System. Which scripts need edits beyond Controls.luau?
Answer
Only CameraClient's zoom handler, which reads the input state; CombatClient and ShopGui keep calling Controls.bind. Everything binds through Controls.bind, so the move touches Controls.luau and the one handler that checks the input state.
Open this step in lesson mode · Link to this step · This confused me
2Move the yard to the Input Action System
In Input everywhere, part 1: keyboard, gamepad, touch and accessibility you bound the swing to every device, met the Input Action System's contexts, actions and bindings, and saw Controls.luau hold every binding in one table. Now you move the whole yard onto that system.
At m14-ship, Controls.luau still calls BindAction. Every client script binds through Controls.bind, so the move touches two game files: Controls.luau itself, and CameraClient, whose zoom handler reads the input state. The public functions (bind, unbind, onPress, inputs) keep their names. What changes is behind them: two contexts, one action per control, and touch buttons you draw. One rule for later: under Server Authority, contexts must sit under the Player so the server knows whose input it is1. The Custom character controllers and physics lesson clones them there.
Build along
src/client/Controls.luausrc/client/CameraClient.luautests/client.luau
In
Controls.luau, keep theBINDINGStable and give each entry acontext:PlayContextfor the first three,MenuContextforCloseShop. Build anInputContextper context under a client-madeReplicatedStorage.Inputsfolder, anInputActionof typeBoolper action, and anInputBindingper key. Build all four actions on the first call, soCloseShopexists before the shop opens.You should see During Play, Explorer shows ReplicatedStorage.Inputs with PlayContext (WrenchSwing, CameraFieldOfView, OpenSettings) and MenuContext (CloseShop), and each action holds one InputBinding per key.
Make a
TextButtonper touch action in aTouchControlsScreenGui, enabled only whileUserInputService.PreferredInputisTouch, and add a binding whoseUIButtonpoints at it.You should see In the device emulator on a phone, round Swing, Zoom and Settings buttons appear on the right, and tapping Swing swings. On a keyboard they are hidden.
Make
bindconnect the handler toPressedand set the action'sEnabled, andunbinddisconnect it. Switch a context on while one of its actions is bound. GiveMenuContextPriority2000 withSinkon, andPlayContext1000.You should see With the Controller Emulator, B closes the open shop; with the shop closed, B does nothing of the yard's.
Change
CameraClient'sonZoomto take no arguments and drop its state check. Then updatetests/client.luau, which reads bindings throughContextActionService, to read them from the actions instead. The challenge's solution has both blocks.You should see The default client suite passes again, and the level 3 check below prints TEST PASS for all seven checks.
Open this step in lesson mode · Link to this step · This confused me
3The note at the front desk stays the same
Both sidesRemember: A RemoteEvent is a note to the front-desk clerk.
The migration changes how the client hears a press. The swing still ends in the same remote with the same arguments, a start point and a direction, and the server never learns which system heard the key. Answer first, then open the breakdown.
Where does it break?“A note to the front-desk clerk” is a good picture of a remoteevent. Where does it stop being true?
It breaks here. The engine stamps the sender for you, but every other word on the note can be a lie.
The true model. A RemoteEvent carries a one-way message between a client and the server. The server always learns which player sent it; every other argument is untrusted input that the server must check.
After the move to the Input Action System, which server code in Beacon Yard has to change?
Answer
None: the Swing remote carries the same arguments and the server checks them the same way. Only the client's way of hearing input changed. The note it sends, and the checks the server runs on it, are the same.
Open this step in lesson mode · Link to this step · This confused me
4Challenges
Level 3, Professional
Challenge
Finish the migration from the build step: every yard control on the Input Action System, nothing left on ContextActionService.
- Start from
- The game at milestone m14 (download)
- Grade it
- In Studio, press Play, then run
game.ServerScriptService.RunGameTests:Invoke("input-actions")in the server command bar. Copy the pass code from Output.
Done means
- ReplicatedStorage.Inputs holds two InputContexts, PlayContext and MenuContext, and MenuContext has the higher Priority and Sink on
- WrenchSwing (F, ButtonR2), CameraFieldOfView (Q, ButtonY) and OpenSettings (P, DPadUp) are Bool InputActions in PlayContext, each with a touch binding whose UIButton is set
- CloseShop (ButtonB, no touch button) is in MenuContext, and no yard action is left in ContextActionService
- challenges/input-actions.luau, run on the client, prints TEST PASS for all seven checks
Hints
- Change only Controls.luau and CameraClient's zoom handler; every other script already calls Controls.bind.
- Build all four actions on the first call, so CloseShop exists before the shop is ever opened.
- Run the test from the client command bar during Play: require(game.ReplicatedStorage.GameTests.challenges["input-actions"]).run(nil)
A player on a phone connects a keyboard and presses F. The TouchControls ScreenGui is enabled only while UserInputService.PreferredInput is Touch. What do they see?
Answer
The touch buttons hide once the keyboard is the preferred input, and F still swings. The buttons show for touch and hide for everything else, while the action keeps its F and ButtonR2 bindings the whole time.
Open this step in lesson mode · Link to this step · This confused me
5Recap
Recap
- Only Controls.luau and CameraClient's zoom handler touch the input API, so the move to the Input Action System changes two files; every other script keeps calling Controls.bind.
- The move means a play context and a higher, sinking menu context, Pressed instead of input states, and touch buttons you draw and link with UIButton.
- Under Server Authority, contexts sit under the Player so the server knows whose input it is.
A RemoteEvent is a note to the front-desk clerk, until the engine stamps the sender for you, but every other word on the note can be a lie.
Queued for review
- You move Controls.bind from BindAction to the Input Action System. Which scripts need edits beyond Controls.luau?
- Why does CloseShop live in its own MenuContext with Sink on, instead of in PlayContext with the swing?
- After the move to the Input Action System, which server code in Beacon Yard has to change?
- A player on a phone connects a keyboard and presses F. The TouchControls ScreenGui is enabled only while UserInputService.PreferredInput is Touch. What do they see?
Open this step in lesson mode · Link to this step · This confused me