How to grab a players textbox input

This is my script

local plr = game.Players.LocalPlayer Input = script.Parent.Text script.Parent.Parent.TextButton.MouseButton1Click:connect(function() print(Input) end)

This is the gui and the explorer
677ecac4803f143ef54fd226739c0e3e
3b4c982130098b85ca475c73a0adf1ca

When i press the click here after input button, it prints but only what the default text is before i play test

I put it new text and press enter but it still prints what it was before i changed it.

Any ideas?

Try this :

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)
1 Like

Yes that works. Is there a way to make it print only when you touch the button?

--Prints when you press the button
script.Parent.Parent.TextButton.MouseButton1Click:connect(function()
   print(Input.Text)
end)
1 Like

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.

1 Like

Final code:

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.

1 Like

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)

image

1 Like