Class · inherits GuiObject
TextBox
A 2D user interface element that displays player-editable text.
About TextBox
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
PlaceholderTextwill be visible. This is useful prompting players of the kind or format of data they should input. - By default, the
ClearTextOnFocusproperty is enabled and ensures there is no existing text when aTextBoxis focused. This may not be desirable for text that should be editable by the player. - The
MultiLineproperty allows players to enter multiple lines of text with newline characters (\n).
Focus State
It is possible to detect and change the focus state of a TextBox:
- You can use
CaptureFocuswhen a dialogue appears so that the player doesn't have to click on aTextBoxwhen it becomes available; you can useContextActionService.BindActionto bind a certain key to focus aTextBoxusing this function. When aTextBoxcomes into focus, theFocusedevent fires. - You can detect if a certain
TextBoxis in focus by usingIsFocused. Alternatively,UserInputService.GetFocusedTextBoxcan be used to check if anyTextBoxis in focus. - When the player is done inputting text, the
FocusLostevent fires, indicating if the player pressed Enter to submit text along with theInputObjectthat caused the loss of focus. When using on-screen keyboards on mobile and console,ReturnPressedFromOnScreenKeyboardmay also fire. - If some more important matter comes up during gameplay, you can
ReleaseFocusof theTextBoxso that a player's keyboard input returns to your game.
Text Editing
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. When
the cursor moves outside the visible area while typing or navigating, the
TextBox automatically scrolls its viewport to keep the cursor in view.
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.
Properties 30
ClearTextOnFocus
: booleanDetermines whether clicking on theTextBoxwill clear itsTextBox.Textproperty.Determines whether clicking on the
TextBoxwill clear itsTextBox.TextpropertyContentText
: stringReadOnlyNotReplicatedA copy ofTextBox.Textthat contains exactly what is being rendered by theTextBox.This property provides a copy of
Textthat contains exactly what is being rendered by theTextBox. This is useful for eliminating style tags used for rich text markup; for example, whenRichTextis enabled, theContentTextproperty shows the text as it appears to the user.CursorPosition
: intDetermines the offset of the text cursor in bytes, or-1if there is no cursor.This property determines the offset of the text cursor in bytes, or
-1if theTextBoxis not currently being edited. A value of1represents the position before the first byte in theTextproperty. When used in conjunction with theSelectionStartproperty, it is possible to both get and set selected text within aTextBox.Note that the units of this property are bytes and that many unicode characters such as emoji are longer than 1 byte. For instance, if a player types in "Hello👋" ("Hello" immediately followed by the waving hand sign), the cursor position would be
10, not7, since the emoji uses 4 bytes.Font
: Enum.FontHiddenNotReplicatedDetermines the font used to render text.This property selects one of several pre-defined
Fontfonts with which the UI element will render its text. Some fonts have bold, italic, and/or light variants.This property is kept in sync with the
TextBox.FontFaceproperty. When settingFont, theFontFaceproperty will be set toDatatype.Font.fromEnum(value).FontFace
: Datatype.FontDetermines the font face used to render text.This property is similar to the
Fontproperty but allows setting fonts that don't exist inFont.This property is kept in sync with the
TextBox.Fontproperty. When settingFontFace, theFontproperty is set to the corresponding enum value, or toFont.Unknownif there are no matches.LineHeight
: floatScales the spacing between lines of text in theTextBox.Controls the height of lines, as a multiple of the font's em square size, by scaling the spacing between lines of text in the
TextBox. Valid values range from1.0(default) to3.0.MaxVisibleGraphemes
: intThe maximum number of graphemes theTextBoxcan show.This property controls the maximum number of graphemes (or units of text) that are shown on the
TextBox, regardless of whether it's showing thePlaceholderTextorText.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
-1disables the limit and shows the entirety of theText.MultiLine
: booleanWhen set totrue, text inside aTextBoxis able to move onto multiple lines.When set to
true, text inside aTextBoxis able to move onto multiple lines. This also enables players to use the Enter key to move onto a new line.OpenTypeFeatures
: stringApplies OpenType typographic features to the text rendered by theTextBox.Comma-separated list of OpenType feature tags applied when the
TextBoxshapes and renders its text, such aszerofor slashed zeros orss03for a stylistic set. Defaults to an empty string (no additional features).Each entry uses standard OpenType feature-string syntax (for example,
zeroorss03). Only a supported subset is applied; an unsupported feature, malformed string, or bracketed partial-enabling syntax (for example,zero[1:3]) is rejected, with the reason inOpenTypeFeaturesError.Supports styling.
OpenTypeFeaturesError
: stringReadOnlyNotReplicatedRead-only property that describes why theTextBox.OpenTypeFeaturesstring could not be applied.Read-only error, if any, from parsing and applying
OpenTypeFeatures. Defaults to an empty string when the requested features applied successfully.An error is produced when:
- The feature string is malformed and cannot be parsed.
- A requested feature is not in the supported set.
- A bracketed range syntax is used (partial feature enabling is not supported).
PlaceholderColor3
: Color3Sets the text color that gets used when no text has been entered into theTextBox.PlaceholderText
: stringSets the text that gets displayed when no text has been entered into theTextBox.RichText
: booleanDetermines whether theTextBoxrenders theTextstring using rich text formatting.This property determines whether the
TextBoxrenders theTextstring using rich text formatting. Rich text uses simple markup tags to style sections of the string in bold, italics, specific colors, and more.Note that when the
TextBoxhas this property enabled and the box gains focus, the player 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.SelectionStart
: intDetermines the starting position of a text selection.Determines the starting position of a text selection, or
-1if theTextBoxhas no range of selected text. This property uses the same positioning logic asCursorPosition.SelectionStartwill be greater thanCursorPositionif the cursor is at the beginning of a selection, and less thanCursorPositionif the cursor is at the end.ShowNativeInput
: booleanIf set totrue, input native to the platform is used instead of Roblox's built-in keyboard.Text
: stringDetermines the string rendered by theTextBoxelement.This property determines the text content rendered by the
TextBoxelement. The visual properties of the string rendered to the screen is determined byTextColor3,TextTransparency,TextSize,FontFace, and other properties.It is possible to render emoji such as 👋 and other symbols. These special symbols aren't affected by the
TextColor3property.This property may contain newline characters, but 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.
TextBounds
: Vector2ReadOnlyNotReplicatedThe size of aTextBoxelement's text in offsets.This read-only property 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.
Using
TextService.GetTextSize, you can predict whatTextBoundswill be on aTextLabelgiven a string,Font,TextSizeand frame size.TextColor3
: Color3Determines the color of non-placeholder rendered text.This property determines the color of non-placeholder text rendered by the
TextBox. Text is rendered after the text stroke.TextDirection
: TextDirectionDirection in which the text is rendered.TextDirectionin which theTextBoxrenders its text. Defaults toTextDirection.Auto, which picks direction from the text content so right-to-left scripts render right-to-left.TextDirection.LeftToRightandTextDirection.RightToLeftforce left-to-right or right-to-left rendering.Supports styling.
TextEditable
: booleanDetermines whether the player can change theText.This property determines whether the player can change the
Textthrough input. It is recommended to disableClearTextOnFocuswhen this property is disabled, otherwise the text can be cleared on-focus.TextFits
: booleanReadOnlyNotReplicatedWhether the text fits within the constraints of theTextBox.TextScaled
: booleanChanges whether text is resized to fit within theTextBox.This property determines whether text is scaled so that it fills the box's entire space. When enabled,
TextSizeis ignored andTextWrappedis automatically enabled. When this property is used for on-screen UI, it may be helpful to use aUITextSizeConstraintto restrict the range of possible text sizes.See
TextLabel.TextScaledfor additional recommendations when using this property.TextSize
: floatDetermine the line height of text in offsets.This property determines the height in offsets of one line of rendered text. The unit is in offsets, not points as in many document editing programs.
TextStrokeColor3
: Color3Determines the color of the text stroke (outline).This property sets the color of the stroke (outline) of the rendered text. This property and
TextStrokeTransparencydetermine the visual appearance of the text stroke.TextStrokeTransparency
: floatDetermines the transparency of the text stroke (outline).This property sets the transparency of the stroke (outline) of the rendered text. This property and
TextStrokeColor3determine the visual appearance of the text stroke.Since text stroke is simply multiple renderings of the same transparency, this property is essentially multiplicative on itself four times over; for example a
TextStrokeTransparencyof0.5appears about the same asTextTransparencyof0.0625, or0.5^4.TextTransparency
: floatDetermines the transparency of the rendered text.This property determines the transparency of all the text rendered by the
TextBox.TextTruncate
: TextTruncateControls the truncation of the text displayed in theTextBox.TextWrapped
: booleanDetermines if text wraps to multiple lines within theTextBoxelement space, truncating excess text.When enabled, this property will render text on multiple lines within a
TextBoxelement's space so thatTextBoundswill never exceed theGuiBase2d.AbsoluteSizeof the element.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 ofGuiBase2d.AbsoluteSize), then that line will not be rendered at all.TextXAlignment
: TextXAlignmentDetermines the horizontal alignment of the rendered text.This property determines the horizontal alignment of text rendered within the
TextBoxelement's space. It can be used in conjunction withTextYAlignmentto fully determine text alignment on both axes.This property does not affect the read-only properties
TextBoundsandTextFits.TextYAlignment
: TextYAlignmentDetermines the vertical alignment of the rendered text.This property determines the vertical alignment of text rendered within the
TextBoxelement's space. It can be used in conjunction withTextXAlignmentto fully determine text alignment on both axes.This property does not affect the read-only properties
TextBoundsandTextFits.
Methods 3
CaptureFocus
() → ()Forces the client to focus on theTextBox.IsFocused
() → booleanWhether the TextBox currently has focus.Returns
trueif theTextBoxis focused orfalseif it is not.ReleaseFocus
(submitted: boolean = false) → ()Forces the client to unfocus the TextBox.Forces the client to unfocus the
TextBox. Thesubmittedparameter lets you override theenterPressedparameter in theTextBox.FocusLostevent.
Events 3
Focused
TextBox.Focused:Connect(function())Fires when theTextBoxgains focus.This event fires when the
TextBoxgains focus, typically when a player clicks/taps on theTextBoxto begin text entry. Also fires ifTextBoxfocus is forced throughCaptureFocus().This event can be used alongside
FocusLostto track when aTextBoxboth gains and loses focus.FocusLost
TextBox.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLoss))Fires when theTextBoxloses its focus.This event fires when the
TextBoxloses its focus, typically when a player clicks/taps outside of it or presses Enter. Also fires ifTextBoxfocus release is forced throughReleaseFocus().This event can be used alongside
FocusLostto track when aTextBoxboth gains and loses focus.ReturnPressedFromOnScreenKeyboard
TextBox.ReturnPressedFromOnScreenKeyboard:Connect(function())Fires when theTextBoxis focused and the player presses the on-screen keyboard's return/enter button.On mobile and console, this event fires when the
TextBoxis focused and the player presses the on-screen keyboard's return/enter button.
TextBox also has every member of GuiObject. Deprecated and Roblox-only members are hidden.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.