Reference API Roblox

Engine API

Website

Related

Reference API Roblox

TextBox

A 2D user interface element that displays player-editable text.

Member index 48

HistoryMember
553ClearTextOnFocus: bool
553ContentText: string
553CursorPosition: int
553FontFace: Font
553FontSize: FontSize
553LineHeight: float
553MaxVisibleGraphemes: int
553MultiLine: bool
629OpenTypeFeatures: string
629OpenTypeFeaturesError: string
553PlaceholderColor3: Color3
486PlaceholderText: string
553RichText: bool
553SelectionStart: int
553ShowNativeInput: bool
553Text: string
553TextBounds: Vector2
553TextColor3: Color3
581TextDirection: TextDirection
553TextEditable: bool
553TextFits: bool
553TextScaled: bool
553TextSize: float
553TextStrokeColor3: Color3
553TextStrokeTransparency: float
553TextTransparency: float
553TextTruncate: TextTruncate
553TextWrap: bool
553TextWrapped: bool
553TextXAlignment: TextXAlignment
553TextYAlignment: TextYAlignment
573CaptureFocus(): null
462IsFocused(): bool
573ReleaseFocus(submitted: bool = false): null
573ResetKeyboardMode(): null
573SetTextFromInput(text: string): null
483FocusLost(enterPressed: bool, inputThatCausedFocusLoss: InputObject)
462Focused()
462ReturnPressedFromOnScreenKeyboard()
inherited from GuiObject
553Active: bool
553AnchorPoint: Vector2
553AutomaticSize: AutomaticSize
553BackgroundColor3: Color3
553BackgroundTransparency: float
553BorderColor3: Color3
553BorderMode: BorderMode
553BorderSizePixel: int
553ClipsDescendants: bool
553Draggable: bool
588GuiState: GuiState
589Interactable: bool
553LayoutOrder: int
553NextSelectionDown: GuiObject
553NextSelectionLeft: GuiObject
553NextSelectionRight: GuiObject
553NextSelectionUp: GuiObject
553Position: UDim2
553Rotation: float
553Selectable: bool
553SelectionImageObject: GuiObject
553SelectionOrder: int
553Size: UDim2
553SizeConstraint: SizeConstraint
553Visible: bool
553ZIndex: int
462TweenPosition(endPosition: UDim2, easingDirection: EasingDirection = Out, easingStyle: EasingStyle = Quad, time: float = 1, override: bool = false, callback: Function = nil): bool
462TweenSize(endSize: UDim2, easingDirection: EasingDirection = Out, easingStyle: EasingStyle = Quad, time: float = 1, override: bool = false, callback: Function = nil): bool
462TweenSizeAndPosition(endSize: UDim2, endPosition: UDim2, easingDirection: EasingDirection = Out, easingStyle: EasingStyle = Quad, time: float = 1, override: bool = false, callback: Function = nil): bool
462DragBegin(initialPosition: UDim2)
462DragStopped(x: int, y: int)
483InputBegan(input: InputObject)
483InputChanged(input: InputObject)
483InputEnded(input: InputObject)
462MouseEnter(x: int, y: int)
462MouseLeave(x: int, y: int)
462MouseMoved(x: int, y: int)
462MouseWheelBackward(x: int, y: int)
462MouseWheelForward(x: int, y: int)
462SelectionGained()
462SelectionLost()
462TouchLongPress(touchPositions: Array, state: UserInputState)
462TouchPan(touchPositions: Array, totalTranslation: Vector2, velocity: Vector2, state: UserInputState)
462TouchPinch(touchPositions: Array, scale: float, velocity: float, state: UserInputState)
462TouchRotate(touchPositions: Array, rotation: float, velocity: float, state: UserInputState)
462TouchSwipe(swipeDirection: SwipeDirection, numberOfTouches: int)
462TouchTap(touchPositions: Array)
inherited from GuiBase2d
553AbsolutePosition: Vector2
553AbsoluteRotation: float
553AbsoluteSize: Vector2
553AutoLocalize: bool
553RootLocalizationTable: LocalizationTable
553SelectionBehaviorDown: SelectionBehavior
553SelectionBehaviorLeft: SelectionBehavior
553SelectionBehaviorRight: SelectionBehavior
553SelectionBehaviorUp: SelectionBehavior
553SelectionGroup: bool
525SelectionChanged(amISelected: bool, previousSelection: GuiObject, newSelection: GuiObject)
inherited from Instance
553Archivable: bool
635Capabilities: SecurityCapabilities
553Name: string
553Parent: Instance
635Sandboxed: bool
616UniqueId: UniqueId
576AddTag(tag: string): null
573ClearAllChildren(): null
462Clone(): Instance
573Destroy(): null
486FindFirstAncestor(name: string): Instance
486FindFirstAncestorOfClass(className: string): Instance
486FindFirstAncestorWhichIsA(className: string): Instance
486FindFirstChild(name: string, recursive: bool = false): Instance
486FindFirstChildOfClass(className: string): Instance
486FindFirstChildWhichIsA(className: string, recursive: bool = false): Instance
486FindFirstDescendant(name: string): Instance
563GetActor(): Actor
486GetAttribute(attribute: string): Variant
462GetAttributeChangedSignal(attribute: string): RBXScriptSignal
631GetAttributes(): Dictionary
648GetChildren(): Instances
462GetDebugId(scopeLength: int = 4): string
486GetDescendants(): Array
486GetFullName(): string
641GetStyled(name: string): Variant
576GetTags(): Array
576HasTag(tag: string): bool
486IsAncestorOf(descendant: Instance): bool
486IsDescendantOf(ancestor: Instance): bool
580IsPropertyModified(name: string): bool
573Remove(): null
576RemoveTag(tag: string): null
580ResetPropertyToDefault(name: string): null
573SetAttribute(attribute: string, value: Variant): null
462WaitForChild(childName: string, timeOut: double): Instance
648children(): Instances
553clone(): Instance
573destroy(): null
553findFirstChild(name: string, recursive: bool = false): Instance
648getChildren(): Instances
553isDescendantOf(ancestor: Instance): bool
573remove(): null
462AncestryChanged(child: Instance, parent: Instance)
462AttributeChanged(attribute: string)
462ChildAdded(child: Instance)
462ChildRemoved(child: Instance)
462DescendantAdded(descendant: Instance)
462DescendantRemoving(descendant: Instance)
500Destroying()
553childAdded(child: Instance)
inherited from Object
647ClassName: string
647className: string
647GetPropertyChangedSignal(property: string): RBXScriptSignal
647IsA(className: string): bool
650isA(className: string): bool
647Changed(property: string)

Removed member index 3

HistoryMember
466Content: string
402IsPassword: bool

Description

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.

Focus State

It is possible to detect and change the focus state of a TextBox:

  • You can use CaptureFocus when a dialogue appears so that the player doesn't have to click on a TextBox when it becomes available; you can use ContextActionService:BindAction() to bind a certain key to focus a TextBox using this function. When a TextBox comes into focus, the Focused event fires.
  • You can detect if a certain TextBox is in focus by using IsFocused. Alternatively, UserInputService:GetFocusedTextBox() can be used to check if any TextBox is in focus.
  • When the player is done inputting text, the FocusLost event fires, indicating if the user pressed Enter to submit text along with the InputObject that caused the loss of focus. When using on screen keyboards on mobile and console, ReturnPressedFromOnScreenKeyboard may also fire.
  • 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.

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.

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.

History 204

Members 48

CaptureFocus

Parameters (0)
No parameters.
Returns (1)
null

Forces the client to focus on the TextBox.

History 4

ClearTextOnFocus

TypeDefault
booltrue

Determines whether clicking on the TextBox will clear its TextBox.Text property

History 4

ContentText

TypeDefault
stringTextBox
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 2

Tags: [ReadOnly, NotReplicated]

CursorPosition

TypeDefault
int1

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.

A visual explanation of how CursorPosition works

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.

History 4

FocusLost

Parameters (2)
enterPressedbool
inputThatCausedFocusLossInputObject

Fires when the client lets their focus leave the TextBox - typically when a client clicks/taps outside of the TextBox. This also fires if a TextBox forces focus on the user.

It can be used alongside TextBox.Focused to track when a TextBox gains and loses focus.

See also the UserInputService.TextBoxFocused and UserInputService.TextBoxFocusReleased for similar functions that rely on the UserInputService service.

This event will only fire when used in a LocalScript.

History 4

Focused

Parameters (0)
No parameters.

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.

See also the UserInputService.TextBoxFocused and UserInputService.TextBoxFocusReleased for similar functions that rely on the UserInputService service.

This event will only fire when used in a LocalScript.

History 2

FontFace

TypeDefault
Font

The FontFace property is similar to the Font property, but allows setting fonts that don't exist in the Font enum.

This property is kept in sync with the TextBox.Font property. When setting FontFace, the Font is set to the corresponding enum value, or to Font.Unknown if there are no matches.

History 2

FontSize

TypeDefault
FontSizeSize8

This property determines the font size of a GUI object.

This property is deprecated. It exists only for backward compatibility, and should not be used for new work. TextSize should be used instead.
This property is not replicated. Its interface does not cross the network boundary.

History 6

Tags: [NotReplicated, Deprecated]

IsFocused

Parameters (0)
No parameters.
Returns (1)
bool

Returns true if the textbox is focused, or false if it is not.

History 2

LineHeight

TypeDefault
float1

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 from 1.0 to 3.0, defaulting to 1.0.

History 4

MaxVisibleGraphemes

TypeDefault
int-1

This property controls the maximum number of graphemes (or units of text) that are shown on the TextBox, regardless of whether it's showing the TextBox.PlaceholderText or TextBox.Text.

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.

History 3

MultiLine

TypeDefault
boolfalse

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.

History 4

OpenTypeFeatures

TypeDefault
string

History 1

OpenTypeFeaturesError

TypeDefault
string
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 1

Tags: [ReadOnly, NotReplicated]

PlaceholderColor3

TypeDefault
Color30.7, 0.7, 0.7

Sets the text color that gets used when no text has been entered into the TextBox yet.

History 6

PlaceholderText

TypeDefault
string

Sets the text that gets displayed when no text has been entered into the TextBox yet.

History 5

ReleaseFocus

Parameters (1)Default
Returns (1)
null

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:

1
2
3
4
5
local TextBox = script.Parent
TextBox.Focused:Connect(function()
	wait(5)
	TextBox:ReleaseFocus()
end)

Please be aware that the above example assumes that it's in a LocalScript, as a child of a TextBox.

History 4

ResetKeyboardMode

Parameters (0)
No parameters.
Returns (1)
null

History 3

ReturnPressedFromOnScreenKeyboard

Parameters (0)
No parameters.

History 2

RichText

TypeDefault
boolfalse

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.

History 4

SelectionStart

TypeDefault
int-1

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.

History 4

SetTextFromInput

Parameters (1)
textstring
Returns (1)
null

History 3

ShowNativeInput

TypeDefault
booltrue

If set to true, input native to the platform is used instead of Roblox's built-in keyboard.

History 4

Text

TypeDefault
stringTextBox

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.

History 4

  • 553 Change Default of Text from to TextBox
  • 486 Change ThreadSafety of Text from ReadOnly to ReadSafe
  • 462 Change ThreadSafety of Text from to ReadOnly
  • 47 Add Text

TextBounds

TypeDefault
Vector20, 0

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.

Using TextService:GetTextSize(), you can predict what TextBounds will be on a TextLabel given a string, TextBox.Font, TextBox.TextSize and frame size.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

TextColor3

TypeDefault
Color30.105882, 0.164706, 0.207843

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.

History 4

TextDirection

TypeDefault
TextDirectionAuto

History 1

TextEditable

TypeDefault
booltrue

TextEditable determines whether the user can change the Text through input. It is recommended to disable ClearTextOnFocus when this property is disabled, otherwise the Text could be cleared on-focus. This property is useful to make read-only TextBoxes from which content can be copied in-game.

History 4

TextFits

TypeDefault
boolfalse

Whether the text fits within the constraints of the TextBox.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

TextScaled

TypeDefault
boolfalse

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.

History 4

TextSize

TypeDefault
float8

The TextSize property determines the height in offsets of one line of rendered text. The unit is in offsets, not points (which is used in most document editing programs). The "Legacy" font does not hold this property.

History 4

TextStrokeColor3

TypeDefault
Color30, 0, 0

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.

History 4

TextStrokeTransparency

TypeDefault
float1

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.

History 4

TextTransparency

TypeDefault
float0

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.

-- Count backwards from 1 to 0, decrementing by 0.1
for i = 1, 0, -0.1 do
   textLabel.TextTransparency = i
   task.wait(0.1)
end

History 4

TextTruncate

TypeDefault
TextTruncateNone

Controls the truncation of the text displayed in this TextBox.

History 4

TextWrap

TypeDefault
boolfalse
This property is deprecated. It exists only for backward compatibility, and should not be used for new work. TextWrapped should be used instead.
This property is not replicated. Its interface does not cross the network boundary.

History 6

Tags: [NotReplicated, Deprecated]

TextWrapped

TypeDefault
boolfalse

When enabled, this property will render text on multiple lines within a GUI element's space so that TextBox.TextBounds will never exceed the GuiBase2d.AbsoluteSize of the GUI 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 of GuiBase2d.AbsoluteSize), then that line will not be rendered at all.

History 4

TextXAlignment

TypeDefault
TextXAlignmentCenter

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.

History 4

TextYAlignment

TypeDefault
TextYAlignmentCenter

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.

History 4

Removed members 3

Content

TypeDefault
string
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 2

Tags: [ReadOnly, NotReplicated]

IsPassword

TypeDefault
bool
This property is not replicated. Its interface does not cross the network boundary.

History 2

Tags: [NotReplicated]

Settings