Codectionary / Developer documentation / Luau

UserInputService

Listen for client keyboard, mouse, touch, or other input events.

Why use UserInputService?

UserInputService reports input received by a player’s client. It is useful for controls and shortcuts; the server cannot rely on a client event as proof that a requested action is allowed.

Try it in Roblox Studio

Put this LocalScript in StarterPlayerScripts and press E in a Studio client. Click an active text field first to see why the processed-input check matters.

Syntax

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent) ... end)

Examples

UserInputService

Recognize the E key when the game has not already processed it.

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then
        return
    end
    if input.KeyCode == Enum.KeyCode.E then
        print("E pressed")
    end
end)
  1. InputBegan fires when an input begins on the client.
  2. gameProcessedEvent helps avoid handling input consumed by UI or another game system.
  3. KeyCode compares against an Enum value. This example prints locally and does not perform an authoritative server action.

Best practices

  • Consider touch and gamepad alternatives for keyboard-only actions.
  • Validate consequential actions after they reach the server.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading