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.

Lab lesson, about 40 minutes, 5 steps

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

Step 1 of 5

What 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.

Choose one

You move Controls.bind from BindAction to the Input Action System. Which scripts need edits beyond Controls.luau?

You move Controls.bind from BindAction to the Input Action System. Which scripts need edits beyond Controls.luau?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 5

Move 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.

Beacon Yard's two input contextsA MenuContext box with CloseShop sits above a PlayContext box holding WrenchSwing, CameraFieldOfView and OpenSettings; an arrow from the menu down to play says B is sunk here while the shop is open.B stops hereMenuContextPriority 2000, Sink: CloseShopPlayContextPriority 1000: Swing, Zoom, Settings
MenuContext hears its keys first and stops them; it is on only while the shop is open.

Build along

m14-ship

  • src/client/Controls.luau
  • src/client/CameraClient.luau
  • tests/client.luau
  1. In Controls.luau, keep the BINDINGS table and give each entry a context: PlayContext for the first three, MenuContext for CloseShop. Build an InputContext per context under a client-made ReplicatedStorage.Inputs folder, an InputAction of type Bool per action, and an InputBinding per key. Build all four actions on the first call, so CloseShop exists 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.

  2. Make a TextButton per touch action in a TouchControls ScreenGui, enabled only while UserInputService.PreferredInput is Touch, and add a binding whose UIButton points 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.

  3. Make bind connect the handler to Pressed and set the action's Enabled, and unbind disconnect it. Switch a context on while one of its actions is bound. Give MenuContext Priority 2000 with Sink on, and PlayContext 1000.

    You should see With the Controller Emulator, B closes the open shop; with the shop closed, B does nothing of the yard's.

  4. Change CameraClient's onZoom to take no arguments and drop its state check. Then update tests/client.luau, which reads bindings through ContextActionService, 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.

Choose one

Why does CloseShop live in its own MenuContext with Sink on, instead of in PlayContext with the swing?

Why does CloseShop live in its own MenuContext with Sink on, instead of in PlayContext with the swing?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 5

The 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.

Choose one

After the move to the Input Action System, which server code in Beacon Yard has to change?

After the move to the Input Action System, which server code in Beacon Yard has to change?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 5

Challenges

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

The Course Runner prints this code when the test passes.

Choose one

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?

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?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 5 of 5

Recap

Recap

  1. 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.
  2. 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.
  3. 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.

Next UI fundamentals: ScreenGui, UDim2, layouts

Link to this step · This confused me