Getting the last sentence in a textbox

I want to make something like a command line, but it got much more advanced than expected.

I’ve got to where it does print out text after pressing Enter, but I can’t manage what would be printing. Now I have a function looking like this:

local function UpdateCurrentText()
	currentText = textBox.Text
end


local function NextLine(enterPressed, _inputObject)
	if enterPressed then
		local guess = currentText
		if guess == currentText .. "F:\Users\VenciB>" then
			textBox.Text = currentText .. "\nF:\Users\VenciB>"
		elseif guess == currentText .. "SecretDIR" then
			textBox.Text = currentText .. "\nOpen notepad.exe?\n[Y] Yes, [N] No\nF:\Users\VenciB>"
		else
			textBox.Text = currentText .. "\nFile or directory not found.\nF:\Users\VenciB>"
			textBox.Text = currentText
		end
	else
		textBox.Text = currentText
	end
end

textBox.FocusLost:Connect(NextLine)
textBox:GetPropertyChangedSignal("Text"):Connect(UpdateCurrentText)

I believe that the problem is with getting the last sentence that was written, it always prints out “File or directory not found.” if you didn’t know by now.

So can anyone explain how I can get the last thing the user has written and forgets the text before it? For instance, after some printing the text in the textbox would look like this:

F:\Users\VenciB>fa
File or directory not found.
F:UsersVenciB>gd
File or directory not found.
F:UsersVenciB>j
File or directory not found.
F:UsersVenciB>
File or directory not found.
F:UsersVenciB>
File or directory not found.
F:UsersVenciB>
File or directory not found.
F:UsersVenciB>
File or directory not found.
F:UsersVenciB> SecretDIR ← something I want to type, still prints the text above

And my intention in the script was it to print something else. So got any ideas?

1 Like

It looks like this in-game

I would use string.find or string.match instead. You might have to edit your whole system:

elseif string.find(guess,"SecretDIR") then

Lemme know if this works:

local function UpdateCurrentText()
     local txt = textBox.text:split('\n')
	currentText = txt[#txt]
end


local function NextLine(enterPressed, _inputObject)
	if enterPressed then
		local guess = currentText
		if guess == currentText .. "F:\Users\VenciB>" then
			textBox.Text = currentText .. "\nF:\Users\VenciB>"
		elseif guess == currentText .. "SecretDIR" then
			textBox.Text = currentText .. "\nOpen notepad.exe?\n[Y] Yes, [N] No\nF:\Users\VenciB>"
		else
			textBox.Text = currentText .. "\nFile or directory not found.\nF:\Users\VenciB>"
			textBox.Text = currentText
		end
	else
		textBox.Text = currentText
	end
end

textBox.FocusLost:Connect(NextLine)
textBox:GetPropertyChangedSignal("Text"):Connect(UpdateCurrentText)

It didn’t, kind of broke it even more actually. After pressing enter it just pastes

F:UsersVenciB>

Doesn’t even move it to the next line.

That worked, but it has a clear flaw.
After typing in SecretDIR it works as it should, but continues the same response over and over, as the text still contains the word. Is there any other function that might be useful?

Yeah you’re going to need to use some string manipulation. A combination of string.len and string.sub. This should work:

local function NextLine(enterPressed, _inputObject)
	if enterPressed then
		local guess = currentText
		local v=string.len(guess)
		if string.sub(guess,1,v) == "F:\Users\VenciB>" then
			textBox.Text = currentText .. "\nF:\Users\VenciB>"
		elseif string.sub(guess,1,v) == "F:\Users\VenciB>SecretDIR" then
			textBox.Text = currentText .. "\nOpen notepad.exe?\n[Y] Yes, [N] No\nF:\Users\VenciB>"
		else
			textBox.Text = currentText .. "\nFile or directory not found.\nF:\Users\VenciB>"
			textBox.Text = currentText
		end
	else
		textBox.Text = currentText
	end
end
1 Like

Thank you for the reply, tested it and it works.
I have though found a solution in the meantime, which also looks less intense:

elseif string.sub(guess, #currentText - 8) == “SecretDIR” then

Simply cut off the last part of the text and check if it matches the string.

My solution is probably basic, so for more configuring, yours would probably be better.

Thank you still.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.