TextBox not outputting anything

I was using a TextBox as a player input device and once I click submit, it prints to the console. But, instead of printing what I want, it prints absolutely nothing. Below is a LocalScript within a ScreenGui.

local TextBox = script.Parent.TextBox
local inputText = TextButton.Text

script.Parent.SubmitButton.Activated:Connect(function()
    print(inputText)
end)

Output:

20:00:40.423 - 

(It literally prints out nothing.)

Is there something wrong with my script, if so please let me know, thanks.

2 Likes

Put your inputText variable inside your function. As it stands, you’re likely assigning inputText to an empty String.

local TextBox = script.Parent.TextBox


script.Parent.SubmitButton.Activated:Connect(function()
    local inputText = TextBox.Text
    print(inputText)
end)

But even if I type stuff inside the textbox, will it still be assigned to an empty string?

@bootsareme

local TextBox = script.Parent.TextBox

script.Parent.SubmitButton.Activated:Connect(function()
    print(tostring(TextBox.Text))
end)

You set the variable for the Text box’s text before the event fired , meaning when that happens (i.e when the event fires , when Activated) the initial text would be displayed " ", which is most likely what your text was initially (nothing).

Index the text after Activated so you get the latest text.
If you type stuff inside the textbox, it will be used directly and printed immediately after the SubmitButton is Activated.

Do you mind showing me how to do that?

That code only makes it so:

  1. It prints out what I initially put there. i.e. before playtesting
  2. During playtesting, When I update the textbox, it still prints out the string I put there before playtesting.

I preparing the place file right now.

demo.rbxl (22.2 KB)

script.Parent.SubmitButton.Activated:Connect(function()
    local text = tostring(script.Parent.TextBox.Text)
    print(text)
end)

Cheers.

@bootsareme

 local TextBox = script:FindFirstAncestor("ScreenGui").TextBox

 script.Parent.MouseButton1Click:Connect(function()
 local text = tostring(TextBox.Text)
 print(text)
 end)

demo.rbxl (22.6 KB)

2 Likes