A TextBox is a rectangle that allows a user to provide text input while it's in focus. When you script a TextBox, you can use it as a search bar or an input field on a form. To help users know what type of text they should input, you can also provide a prompt through the PlaceholderText property.
Because these objects are GuiObjects, you can customize properties such as BackgroundColor3, BorderMode, Transparency, and Rotation to fit the aesthetics of your experience.
Creating Text Inputs on the Screen
A TextBox on a screen is useful for things like an input field for a form.
To add a TextBox to a screen:
In the Explorer window, select StarterGui and add a ScreenGui.
Hover over StarterGui and click the ⊕ button. A contextual menu displays.
Insert a ScreenGui.
Select the new ScreenGui and add a TextBox.
Hover over ScreenGui and click the ⊕ button. A contextual menu displays.
Insert a TextBox.
Creating Text Inputs on Part Faces
To add a TextBox to the face of a part:
In the Explorer window, select StarterGui and add a SurfaceGui.
Hover over StarterGui and click the ⊕ button. A contextual menu displays.
Insert a ScreenGui.
Select the new SurfaceGui and add a TextBox.
Hover over SurfaceGui and click the ⊕ button. A contextual menu displays.
Insert a TextBox.
Adorn the SurfaceGui to the part on which you want to display the TextBox.
In the Properties window, select the Adornee property. Your cursor changes.
In the Explorer window, select the part.
Scripting Text Inputs
Like buttons, you can script any action for a TextBox object when a user interacts with it. For example, the following script connects the FocusLost event which fires when the user presses the Enter button or clicks outside the box. If enterPressed is true, meaning the user submitted the input instead of merely clicking outside the box, the script prints the contents of the entry to the Output window.
Basic Text Input Handling
local textBox = script.Parent
local function onFocusLost(enterPressed, inputObject)
if enterPressed then
print(textBox.Text)
end
end
textBox.FocusLost:Connect(onFocusLost)
As another example, you might want to allow only numbers in a TextBox. The following code uses the TextBox.GetPropertyChangedSignal event to detect when the TextBox.Text changes, such as when a user starts typing, then it uses the string.gsub() function to disallow non‑numbers.
Restricting Text Input to Numbers
local textBox = script.Parent
local function allowOnlyNumbers()
textBox.Text = string.gsub(textBox.Text, "%D", "")
end
textBox:GetPropertyChangedSignal("Text"):Connect(allowOnlyNumbers)
Text Filtering
Applied to various sources and inputs, text filtering prevents users from seeing inappropriate language and personally identifiable information such as phone numbers. Roblox automatically filters common text outputs such as messages that have passed through in-experience text chat, but you are responsible for filtering any displayed text that you don't have explicit control over, including characters/strings that users input through text inputs.