Trying to create an input/output box

Hi.

I’m trying to create a script that, when the player presses enter after typing specific text in a text box, deletes the text and sets the text in another text box to something else. An example would be if I typed “will it work?” in an input box, and that text would get deleted and “it works!!!” would appear in an output box. My problem is, when I press enter, the text just stays in the box, like the event doesn’t fire (I think it’s cause the game thinks i’m submitting text to a text box and nothing else). All the UserInputService and ContextActionService tutorials I found left me with more questions than answers, so I just tried this

local terminal = script.Parent
local outputBox = terminal.Parent.OutputBox
local output = outputBox.Text
local terminalInput = terminal.Text

function onInput()
	if terminalInput == "will it work?" then
		terminalInput = ""
		output = "it works!!!"
	else
		terminalInput = ""
		output = "invalid command"
	end
end

terminal.FocusLost:Connect(onInput)

in a LocalScript and that didn’t work either! If there’s any useful lua knowledge I’m missing, please inform me. Thanks!

Since you set the tarminalInput variable at the top, it is never going to be updated to what’s actually in the TextBox, it’s only set the first time the script runs. You’ll either need to set that variable inside the function so it’s up to date each time it’s called, or just don’t use the variable at all like so

local terminal = script.Parent
local outputBox = terminal.Parent.OutputBox

function onInput()
	if terminal.Text == "will it work?" then
		terminal.Text = ""
		outputBox.Text = "it works!!!"
	else
		terminal.Text = ""
		outputBox.Text = "invalid command"
	end
end

terminal.FocusLost:Connect(onInput)
1 Like

Not setting the variable worked! Thanks! :grin:

1 Like