React to raw input from the keyboard, mouse, touch, and gamepad with UserInputService events.
Buttons and ClickDetectors cover clicking. But what about movement keys, the Escape menu, or gamepad buttons? For raw input — any key, any button, any device — you reach for UserInputService.
UserInputService is a singleton: one object, already there, that reports every input a makes.
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
print("Something was pressed!")
end)
InputBegan fires the moment any input starts — a key going down, a mouse button, a tap, a gamepad button. It's an , just like the ones you connected in the lesson: you're registering a callback the engine calls for you.
UserInputService only works on the client. It must live in a LocalScript — the server has no idea what keys any is pressing, and that's on purpose.
The callback receives an InputObject — a snapshot of that single input. Its two most useful are KeyCode and UserInputType.
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.Space then
print("Space pressed!")
end
end)
KeyCode — which button. Enum.KeyCode.Space, Enum.KeyCode.W, Enum.KeyCode.Escape.UserInputType — what kind of device. Enum.UserInputType.Keyboard, MouseButton1, Touch, Gamepad1.Check KeyCode when you care which key; check UserInputType when you care which kind of device.
Which property tells you WHICH key was pressed?
The second argument answers one question: did Roblox already use this input? When a clicks a GUI button, the UI "consumes" that click — you usually don't want your movement code firing too.
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if input.KeyCode == Enum.KeyCode.Space then
print("Space pressed!")
end
end)
return early to skip anything the UI handled. This is the #1 guard in input — skip it and typing in a chat box or clicking a button will also trigger your code.
Mouse movement is a different , InputChanged, which fires every frame the mouse moves. It's for camera control and aiming, not for discrete presses.
To skip inputs that Roblox already used, check if is true and return early.
Here's the whole pattern, wired up:
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if input.UserInputType == Enum.UserInputType.Keyboard then
print("Key pressed: " .. input.KeyCode.Name)
end
end
UserInputService.InputBegan:Connect(onInputBegan)
A named keeps the Connect line tidy, and .KeyCode.Name gives you a readable string ("Space", "W") for printing.
Connect to InputBegan with a callback that ignores processed events and prints 'JUMP' when the Space key is pressed.
local UserInputService = game:GetService("UserInputService")
-- your connect here
gameProcessedEvent guard. Clicking a button or typing in chat also fires InputBegan. Without the guard, your movement or jump code fires when the is just using the UI.input.KeyCode == "Space". KeyCodes are enums, not strings — compare against Enum.KeyCode.Space, never a quoted name.while true.Next: naming your actions instead of your keys.