local plr = game:GetService("Players").LocalPlayer
local Input = script.Parent
--Prints whenever the text inside 'Input' changes
Input:GetPropertyChangedSignal("Text"):Connect(function()
print(Input.Text)
end)
--Prints when you press the button
script.Parent.Parent.TextButton.MouseButton1Click:connect(function()
print(Input.Text)
end)
I recommend making the button call the ReleaseFocus function.
This allows you to pass true for the submitted parameter, which would fire the FocusLost event with enterPressed as true, just as if they pressed enter to release focus. That allows you to cover the case of pressing enter or clicking submit by just listening to the FocusLost event and acting if enterPressed is true.
local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local Input = script.Parent
--Prints whenever the text inside 'Input' changes
Input:GetPropertyChangedSignal("Text"):Connect(function()
print(Input.Text)
end)
--Prints when you press the button
script.Parent.Parent.TextButton.MouseButton1Click:connect(function()
print(Input.Text)
end)
If you want it to be as simple as possible - this should be fine.
If you want a different method, you might want to use ReleaseFocus etc.
Here’s an example using FocusLost, which allows the user to press Enter or click the button:
local textBox = script.Parent.TextBox
local textButton = script.Parent.TextButton
textBox.FocusLost:Connect(function(enterPressed: boolean)
if not enterPressed then
return
end
print("Submitted", textBox.Text)
end)
textButton.Activated:Connect(function(inputObject: InputObject)
if not inputObject.UserInputState == Enum.UserInputState.Begin then
return
end
-- Capturing focus to make sure the .FocusLost event fires. Make sure
-- ClearTextOnFocus is disabled, otherwise this would clear the text.
textBox:CaptureFocus()
textBox:ReleaseFocus(true)
end)