Learn
Engine Class
TextBox

Code Samples
TextBox Secret Word
-- Place this code in a LocalScript inside a TextBox
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- white
local colorWrong = Color3.new(1, 0, 0) -- red
local colorCorrect = Color3.new(0, 1, 0) -- green
-- Initialize the state of the textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "What is the secret word?"
textBox.BackgroundColor3 = colorNormal
local function onFocused()
textBox.BackgroundColor3 = colorNormal
end
local function onFocusLost(enterPressed, _inputObject)
if enterPressed then
local guess = textBox.Text
if guess == secretWord then
textBox.Text = "ACCESS GRANTED"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "ACCESS DENIED"
textBox.BackgroundColor3 = colorWrong
end
else
-- The player stopped editing without pressing Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)

API Reference
Properties
ClearTextOnFocus
Read Parallel
Capabilities: UI
TextBox.ClearTextOnFocus:boolean

ContentText
Read Only
Not Replicated
Read Parallel
Capabilities: UI
TextBox.ContentText:string

CursorPosition
Read Parallel
Capabilities: UI
TextBox.CursorPosition:number
Code Samples
TextBox Selections
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("No selection")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('The selection is:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)

Font
Hidden
Not Replicated
Read Parallel
Capabilities: UI
TextBox.Font:Enum.Font

FontFace
Read Parallel
Capabilities: UI
TextBox.FontFace:Font

FontSize
Deprecated

LineHeight
Read Parallel
Capabilities: UI
TextBox.LineHeight:number

MaxVisibleGraphemes
Read Parallel
Capabilities: UI
TextBox.MaxVisibleGraphemes:number

MultiLine
Read Parallel
Capabilities: UI
TextBox.MultiLine:boolean

OpenTypeFeatures
Read Parallel
Capabilities: UI
TextBox.OpenTypeFeatures:string

OpenTypeFeaturesError
Read Only
Not Replicated
Read Parallel
Capabilities: UI
TextBox.OpenTypeFeaturesError:string

PlaceholderColor3
Read Parallel
Capabilities: UI
TextBox.PlaceholderColor3:Color3

PlaceholderText
Read Parallel
Capabilities: UI
TextBox.PlaceholderText:string

RichText
Read Parallel
Capabilities: UI
TextBox.RichText:boolean

SelectionStart
Read Parallel
Capabilities: UI
TextBox.SelectionStart:number
Code Samples
TextBox Selections
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("No selection")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('The selection is:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)

ShowNativeInput
Read Parallel
Capabilities: UI
TextBox.ShowNativeInput:boolean

Text
Read Parallel
Capabilities: UI
TextBox.Text:string

TextBounds
Read Only
Not Replicated
Read Parallel
Capabilities: UI
TextBox.TextBounds:Vector2

TextColor
Deprecated

TextColor3
Read Parallel
Capabilities: UI
TextBox.TextColor3:Color3
Code Samples
Vowel Detector
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Check for vowels
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Black
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Red
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)

TextDirection
Read Parallel
Capabilities: UI
TextBox.TextDirection:Enum.TextDirection

TextEditable
Read Parallel
Capabilities: UI
TextBox.TextEditable:boolean

TextFits
Read Only
Not Replicated
Read Parallel
Capabilities: UI
TextBox.TextFits:boolean

TextScaled
Read Parallel
Capabilities: UI
TextBox.TextScaled:boolean

TextSize
Read Parallel
Capabilities: UI
TextBox.TextSize:number

TextStrokeColor3
Read Parallel
Capabilities: UI
TextBox.TextStrokeColor3:Color3

TextStrokeTransparency
Read Parallel
Capabilities: UI
TextBox.TextStrokeTransparency:number

TextTransparency
Read Parallel
Capabilities: UI
TextBox.TextTransparency:number

TextTruncate
Read Parallel
Capabilities: UI
TextBox.TextTruncate:Enum.TextTruncate

TextWrap
Deprecated

TextWrapped
Read Parallel
Capabilities: UI
TextBox.TextWrapped:boolean

TextXAlignment
Read Parallel
Capabilities: UI
TextBox.TextXAlignment:Enum.TextXAlignment

TextYAlignment
Read Parallel
Capabilities: UI
TextBox.TextYAlignment:Enum.TextYAlignment

Methods
CaptureFocus
Capabilities: UI
TextBox:CaptureFocus():()
Returns
()
Code Samples
Capture TextBox Focus
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Q then
textBox:CaptureFocus()
end
end)

IsFocused
Capabilities: UI
TextBox:IsFocused():boolean
Returns

ReleaseFocus
Capabilities: UI
TextBox:ReleaseFocus(submitted:boolean):()
Parameters
submitted:boolean
Default Value: false
Returns
()
Code Samples
Release TextBox Focus
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape and textBox.IsFocused then
textBox:ReleaseFocus()
end
end)

Events
Focused
Capabilities: UI
TextBox.Focused():RBXScriptSignal
Code Samples
TextBox Focus
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Focused!")
end)
textBox.FocusLost:Connect(function()
print("Focus lost!")
end)

FocusLost
Capabilities: UI
TextBox.FocusLost(
enterPressed:boolean, inputThatCausedFocusLoss:InputObject
Parameters
enterPressed:boolean
inputThatCausedFocusLoss:InputObject
Code Samples
TextBox Focus
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Focused!")
end)
textBox.FocusLost:Connect(function()
print("Focus lost!")
end)
FocusLost
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Player pressed Enter")
else
print("Player pressed", inputThatCausedFocusLost.KeyCode)
end
end
textBox.FocusLost:Connect(onFocusLost)
TextBox.FocusLost1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("Focus was lost because enter was pressed!")
else
print("Focus was lost without enter being pressed")
end
end
textBox.FocusLost:Connect(focusLost)

ReturnPressedFromOnScreenKeyboard
Capabilities: UI
TextBox.ReturnPressedFromOnScreenKeyboard():RBXScriptSignal

©2026 Roblox Corporation. Roblox, the Roblox logo and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.