Kelas Engine
TextBox
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
Rangkuman
Properti
Metode
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
Acara
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
Contoh Kode
Kotak Teks Kata Sandi
-- Tempatkan kode ini dalam LocalScript di dalam Kotak Teks
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- putih
local colorWrong = Color3.new(1, 0, 0) -- merah
local colorCorrect = Color3.new(0, 1, 0) -- hijau
-- Inisialisasi keadaan textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "Apa kata sandi rahasianya?"
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 = "AKSES DIIZINKAN"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "AKSES DITOLAK"
textBox.BackgroundColor3 = colorWrong
end
else
-- Pemain berhenti mengedit tanpa menekan Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)Referensi API
Properti
CursorPosition
Contoh Kode
Pilihan TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Tidak ada pilihan")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('Pilihan adalah:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
Contoh Kode
Pilihan TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Tidak ada pilihan")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('Pilihan adalah:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
Contoh Kode
Detektor Vokal
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Periksa vokal
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Hitam
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Merah
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
Metode
CaptureFocus
TextBox:CaptureFocus():()
Memberikan nilai
()
Contoh Kode
Mengambil Fokus TextBox
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)ReleaseFocus
Parameter
| Nilai Default: false |
Memberikan nilai
()
Contoh Kode
Lepaskan Fokus TextBox
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)Acara
Focused
Contoh Kode
Fokus TextBox
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Fokus!")
end)
textBox.FocusLost:Connect(function()
print("Fokus hilang!")
end)FocusLost
Parameter
Contoh Kode
Fokus TextBox
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Fokus!")
end)
textBox.FocusLost:Connect(function()
print("Fokus hilang!")
end)Fokus Hilang
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Pemain menekan Enter")
else
print("Pemain menekan", 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("Fokus hilang karena tombol enter ditekan!")
else
print("Fokus hilang tanpa tombol enter ditekan")
end
end
textBox.FocusLost:Connect(focusLost)