Intro to Player Input
Intro to Player Input
Every game needs to receive player input in some way. On Roblox, there are various ways to process input using
articles/events|events. Before continuing, you should be fairly comfortable with using
articles/events|events since input processing is almost entirely event-based.
articles/ContextActionService Creating Mobile Buttons|Creating Mobile Buttonsand
articles/Gamepad Input|Gamepad Inputfor information on phone/tablet and gamepad input.
Input on 3D Objects
ClickDetector object is perhaps the simplest form of input on Roblox. It detects mouse clicks (or taps) on 3D objects and fires its
ClickDetector/MouseClick|MouseClick event. You can insert a click detector in Studio or use
Instance.new() to begin detecting player clicks on a
Click detectors work in either a
LocalScript. They also have three other events:
||Fires when the mouse pointer hovers over the detector object. On mobile devices, this only fires when the player initially taps the detector object to "activate" it, and when they drag their finger off and back over the detector during a subsequent touch.|
||Fires when the mouse pointer hovers off the detector object. On mobile, this only fires when the player initially taps the detector object to "activate" it and then either taps somewhere off the detector, or drags their finger off the detector following a subsequent touch.|
||Fires when the player right-clicks the mouse over the detector object. This event does not function on mobile devices.|
Generic Mouse Input
Perhaps you need generic mouse input instead of one object in particular. For this, you can use the
Player/GetMouse|Player:GetMouse() function which returns a
PlayerMouse object. This houses a handful of useful mouse properties and events.
Player/GetMouse|GetMouse()only works when using a
LocalScript, so you should access
Players/LocalPlayerto get a reference to the local player and parent the script to StarterPlayerScripts (in StarterPlayer) so that it later runs when copied to
The keyboard is a wonderful tool for exposing many options to the player at once. The easiest way to access keyboard events is through the
UserInputService/InputEnded event which fires when any general user input ends. Like mouse events, this only works inside a
User Interface Input
All on-screen user interfaces house their own input events. Button-type objects like
ImageButton|ImageButtons are particularly useful for single-action input. See
/articles/Creating GUI Buttons|Creating GUI Buttons for details and examples.
TextBox is a special UI object designed to capture text input, like a text field in a form. Its
TextBox/Text|Text property is changed as the player types into it, and the
TextBox/FocusLost|FocusLost event fires after the player is done typing.
- Insert a new
- Insert a
ScreenGui, then create a new
LocalScriptas a child of the
- Paste the following code into the script — this will set the background color of the
TextBoxto a valid user-typed
/datatype/BrickColor|BrickColor, for instance “Bright red” or “Electric blue.”
articles/Text and Chat Filtering|Text and Chat Filteringarticle for more information.