A TextBox allows the player to provide text input. It behaves similarly to a TextButton, except that a single TextBox can be put in focus by clicking, tapping or gamepad selection. While in focus, the player can use a keyboard to change the Text property.
- If there is no text, the PlaceholderText will be visible. This is useful prompting players of the kind or format of data they should input.
- By default, the ClearTextOnFocus property is enabled and ensures there is no existing text when a TextBox is focused. This may not be desirable for text that should be editable by the player.
- The MultiLine property allows players to enter multiple lines of text with newline characters (\n).
The ContextActionService honors TextBox keybinds and will automatically prevent key press events from being passed to actions bound with ContextActionService:BindAction(). UserInputService.InputBegan and related events will still fire while a TextBox is in focus.
It is possible to detect and change the focus state of a TextBox:
- If some more important matter comes up during gameplay, you can ReleaseFocus of the TextBox so that a player's keyboard input returns to your game.
A TextBox supports text selection through its CursorPosition and SelectionStart properties. Using GetPropertyChangedSignal, you can detect when a selection changes. Additionally, it is possible for players to copy and paste text within a TextBox, enabling basic clipboard support.
Text Filtering Notice Games that facilitate player-to-player communication using text, such as custom chat or nametags, must properly filter such text using TextService:FilterStringAsync() or Chat:FilterStringAsync(). If this is not properly done, your game may receive moderation action.
1-- Place this code in a LocalScript inside a TextBox2local textBox = script.Parent34local secretWord = "roblox"5local colorNormal = Color3.new(1, 1, 1) -- white6local colorWrong = Color3.new(1, 0, 0) -- red7local colorCorrect = Color3.new(0, 1, 0) -- green89-- Initialize the state of the textBox10textBox.ClearTextOnFocus = true11textBox.Text = ""12textBox.Font = Enum.Font.Code13textBox.PlaceholderText = "What is the secret word?"14textBox.BackgroundColor3 = colorNormal1516local function onFocused()17 textBox.BackgroundColor3 = colorNormal18end1920local function onFocusLost(enterPressed, _inputObject)21 if enterPressed then22 local guess = textBox.Text23 if guess == secretWord then24 textBox.Text = "ACCESS GRANTED"25 textBox.BackgroundColor3 = colorCorrect26 else27 textBox.Text = "ACCESS DENIED"28 textBox.BackgroundColor3 = colorWrong29 end30 else31 -- The player stopped editing without pressing Enter32 textBox.Text = ""33 textBox.BackgroundColor3 = colorNormal34 end35end3637textBox.FocusLost:Connect(onFocusLost)38textBox.Focused:Connect(onFocused)
Determines whether clicking on the TextBox will clear its TextBox.Text property.
Determines the offset of the text cursor in bytes, or -1 if there is no cursor.
When set to true, text inside a TextBox is able to move onto multiple lines. This also enables players to use the enter key to move onto a new line.
Sets the text color that gets used when no text has been entered into the TextBox yet.
Sets the text that gets displayed when no text has been entered into the TextBox yet.
Determines whether the TextBox renders the TextBox.Text string using rich text formatting.
Determines the starting position of a text selection, or -1 if no text is selected.
If set to true, input native to the platform is used instead of Roblox's built-in keyboard.
Whether the text fits within the constraints of the TextBox.
Determines if text wraps to multiple lines within the GUI element space, truncating excess text.
CursorPosition determines the offset of the text cursor in bytes, or -1 if the TextBox is not currently being edited. A value of 1 represents the beginning, the position before the first byte in the Text property. When used in conjunction with the SelectionStart property, it is possible to both get and set selected text within a TextBox.
It should be noted that the units of this property is bytes and that many unicode characters such as emoji are longer than 1 byte. For instance, if a player types into the TextBox "Hello👋" – "Hello" immediately followed by the waving hand sign – the cursor position would be 10, not 7, since the emoji uses 4 bytes.
The Font property selects one of several pre-defined fonts with which the UI element will render its text. Some fonts have bold, italic and/or light variants (as there is no font-weight or font-style properties).
With the exception of the "Legacy" font, each font will render text with the line height equal to the TextBox.TextSize property. The "Code" font is the only monospace font. It has the unique property that each character has the exact same width and height ratio of 1:2. The width of each character is approximately half the TextBox.TextSize property.
Changing the property does not change the position or size of the visible graphemes - the layout will be calculated as if all graphemes are visible.
Setting the property to -1 disables the limit and shows the entirety of the TextBox.Text.
This property determines whether the TextBox renders the TextBox.Text string using rich text formatting. Rich text uses simple markup tags to style sections of the string in bold, italics, specific colors, and more.
To use rich text, simply include formatting tags in the TextBox.Text string.
Note that when the TextBox has this property enabled and the box gains focus, the user will be able to edit and interact with the complete XML string, including all of the formatting tags. When focus is lost, the text will automatically parse and render the tags as rich text.
Determines the starting position of a text selection, or -1 if the TextBox has no range of selected text. If the value is -1 or equivalent to CursorPosition, there is no range of text selected. This property uses the same positioning logic as CursorPosition. SelectionStart will be greater than CursorPosition if the cursor is at the beginning of a selection, and less than CursorPosition if the cursor is at the end.
The Text property determines the content rendered by the UI element. The visual properties of the string rendered to the screen is determined by TextBox.TextColor3, TextBox.TextTransparency, TextBox.TextSize, TextBox.Font, TextBox.TextScaled, TextBox.TextWrapped, TextBox.TextXAlignment and TextBox.TextYAlignment.
It is possible to render emoji (for example, 😃) and other symbols. These special symbols aren't affected by the TextBox.TextColor3 property. These can be pasted into Script and LocalScript objects, as well as the field within the Properties window.
This property may contain newline characters, however, it is not possible to type newline characters within the Properties window. Similarly, this property may contain a tab character, but it will render as a space instead.
The read-only property TextBounds reflects the absolute size of rendered text in offsets. In other words, if you were to try to fit text into a rectangle, this property would reflect the minimum dimensions of the rectangle you would need in order to fit the text.
This property determines the color of all the text rendered by a GUI element. This property along with TextBox.Font, TextBox.TextSize and TextBox.TextTransparency will determine the visual properties of text. Text is rendered after the text stroke (TextBox.TextStrokeColor3).
It's important that text is easily read by players! Be sure to choose colors with little-to-no saturation, like white, grey, or black. Make sure the color of your text is contrasted by the TextBox.BackgroundColor3 of the UI element. If the element has a transparent background, try applying a black TextBox.TextStrokeColor3 to help contrast the text with the 3D world behind it.
Rather than using TextScaled, we recommend you consider using AutomaticSize, a new method to dynamically size UI that will give you the best visual result possible.
The TextScaled property determines whether text is scaled so that it fills the entire UI element's space. When this is enabled, TextBox.TextSize is ignored and TextBox.TextWrapped is automatically enabled. This property is useful for text-rendering UI elements within BillboardGuis.
When this property is used for screen-space UI, it may be desirable to use a UITextSizeConstraint to restrict the range of possible text sizes.
TextScaled and AutomaticSize
It's recommended that developers avoid usage of TextScaled and adjust UI to take advantage of the AutomaticSize property instead. Here are the core differences between the two properties:
- TextScaled scales the content (text) to accommodate the UI. Without careful consideration, some text may become unreadable if scaled too small.
- AutomaticSize resizes the UI to accommodate content.
With AutomaticSize, you're able to adjust your UI to accommodate the content (text) while maintaining a consistent font size. For more information on how to use automatic sizing, see the UI Automatic Size article.
We suggest that you don't apply both TextScaled and AutomaticSize on the same UI object. If you apply both properties:
- AutomaticSize determines the maximum amount of available space that a GuiObject can use (in this case, text)
- TextScaled uses the available space determined by AutomaticSize, to scale the font size to fit the available space, which will expand up to the maximum font size (100), if there are no size constraints
- The end result will be: text goes to 100 font size and the UI object will expand to fit that text
Using both AutomaticSize and TextScaled at the same time can result in significant scaling differences than when AutomaticSize is off. Here is an example of an automatically sized TextLabel (with no minimum size) that has TextScaled enabled:
Note how automatic size changes the TextLabel's size relative to the parent frame's size. Subsequently, as the TextLabel is resized, the TextScaled property scales the text to the maximum amount of space available by the automatically sized TextLabel.
The TextStrokeColor3 property sets the color of the stroke, or outline, of rendered text. This property and TextBox.TextStrokeTransparency determine the visual properties of the text stroke.
Text stroke is rendered before normal text and is simply 4 renderings of the same text in +/- 1 pixel offsets in each direction. Text stroke rendering works independently and identically to TextBox.TextColor3 and TextBox.TextTransparency.
The TextStrokeTransparency property sets the transparency of the stroke, or outline, of rendered text. This property and TextBox.TextStrokeColor3 determine the visual properties of the text stroke.
Text stroke is rendered before normal text and is simply 4 renderings of the same text in +/- 1 pixel offsets in each direction. Text stroke rendering works independently and identically to TextBox.TextColor3 and TextBox.TextTransparency. Since text stroke is simply multiple renderings of the same transparency, this property is essentially multiplicative on itself four times over (e.g. a TextStrokeTransparency of 0.5 appears about the same as TextTransparency of 0.0625, or 0.5^4). Therefore, it's recommended to set TextStrokeTransparency to a value in the range of 0.75 to 1 for more a more subtle effect.
The TextColor3 property determines the transparency of all the text rendered by a UI element. This property along with TextBox.Font, TextBox.TextSize and TextBox.TextColor3 will determine the visual properties of text. Text is rendered after the text stroke (TextBox.TextStrokeTransparency).
Fading text in using a numeric for-loop is a fantastic way to draw a player's attention to text appearing on screen.
1-- Count backwards from 1 to 0, decrementing by 0.12for i = 1, 0, -.1 do3 textLabel.TextTransparency = i4 wait(.1)5end6
This is achieved by breaking long lines of text into multiple lines. Line breaks will prefer whitespace; should a long unbroken word exceed the width of the element, that word will be broken into multiple lines.
If further line breaks would cause the vertical height of the text (the Y component of TextBox.TextBounds) to exceed the vertical height of the element (the Y component of GuiBase2d.AbsoluteSize), then that line will not be rendered at all.
TextXAlignment determines the horizontal alignment (X-axis) of text rendered within a UI element's space. It functions similarly to the CSS text-align property, with left, right and center values (there is no justify option). For Left and Right, text is rendered such that the left/right text bounds just touch the edge of the UI element rectangle. For Center, each line of text is centered on the very center of the UI element rectangle.
This property is used in conjunction with TextBox.TextYAlignment to fully determine text alignment on both axes. This property won't affect the read-only properties TextBox.TextBounds and TextBox.TextFits.
TextYAlignment determines the vertical alignment (Y-axis) of text rendered within a UI element's space. For Top and Bottom, text is rendered such that the top/bottom text bounds just touch the edge of the UI element rectangle. For Center, text is rendered such that there is an equal space from the top bounds of the text to the top of the element and the bottom bounds of the text to the bottom of the element.
This property is used in conjunction with TextBox.TextXAlignment to fully determine text alignment on both axes. This property won't affect the read-only properties TextBox.TextBounds and TextBox.TextFits.
The FocusLost event fires when the client lets their focus off the TextBox - typically when a client clicks/taps on a TextBox to begin text entry. This also fires if a TextBox forces focus on the user.
It can be used alongside TextBox.Focus to track when a TextBox gains and loses focus.
This event will only fire when used in a LocalScript.
1local textBox = script.Parent23local function onFocusLost(enterPressed, inputThatCausedFocusLost)4 if enterPressed then5 print("Player pressed Enter")6 else7 print("Player pressed", inputThatCausedFocusLost.KeyCode)8 end9end1011textBox.FocusLost:Connect(onFocusLost)
1local function focusLost(enterPressed)2 if enterPressed then3 print("Focus was lost because enter was pressed!")4 end5end67script.Parent.FocusLost:Connect(focusLost)
The Focused event fires when the TextBox gains focus - typically when a client clicks/taps on a TextBox to begin text entry. This also fires if a TextBox forces focus on the user.
It can be used alongside TextBox.FocusLost to track when a TextBox gains and loses focus.
This event will only fire when used in a LocalScript.
1local textBox = script.Parent23local function onFocused()4 print("Focused")5end67textBox.Focused:Connect(onFocused)
Forces the client to focus on the TextBox.
1local ContextActionService = game:GetService("ContextActionService")23local ACTION_NAME = "FocusTheTextBox"45local textBox = script.Parent67local function handleAction(actionName, inputState, _inputObject)8 if actionName == ACTION_NAME and inputState == Enum.UserInputState.Begin then9 textBox:CaptureFocus()10 end11end1213ContextActionService:BindAction(ACTION_NAME, handleAction, false, Enum.KeyCode.Q)
Forces the client to unfocus the TextBox. The submitted parameter allows you to over-ride the enterPressed parameter in the TextBox.FocusLost event.
This item should be used with a LocalScript in order to work as expected in online mode.
The code shown below will force the client to unfocus the 'TextBox' 5 seconds after it's selected:
1local TextBox = script.Parent2TextBox.Focused:Connect(function()3 wait(5)4 TextBox:ReleaseFocus()5end)6
Please be aware that the above example assumes that it's in a LocalScript, as a child of a TextBox.
1local textBox = script.Parent23local function onFocused()4 task.wait(5)5 textBox:ReleaseFocus()6end78textBox.Focused:Connect(onFocused)