Typewriter module not returning one text string

So I have a modified typewriter module, and it’s modified to auto-complete the text once the player presses enter. It works, except when the player does press enter, and more than one string has been run through the module, it auto-completes to what the first string sent through was. How do I make it so the function forgets about any previous strings sent through? Any help is appreciated!

Function:

local enterPressed = false

function TypeWriter.typeWrite(guiObject, text)
	guiObject.AutoLocalize = false
	guiObject.Text = ""
	local displayText = text
	if translator then
		displayText = translator:Translate(guiObject, text)
	end
	for first, last in utf8.graphemes(displayText) do
		if enterPressed ~= true then
			UIS.InputBegan:Connect(function(input)
				if guiObject.Text ~= " " then
					if input.KeyCode == Enum.KeyCode.Return then
						guiObject.Text = text
						enterPressed = true
					end
				end
			end)
			local grapheme = string.sub(displayText, first, last)
			guiObject.Text = guiObject.Text .. grapheme
			if defaultConfigurations.extraDelayOnSpace and grapheme == " " then
				wait(defaultConfigurations.delayTime)
			end
			if defaultConfigurations.extraDelayOnComma and grapheme == "," then
				wait(defaultConfigurations.commaDelay)
			end
			if defaultConfigurations.extraDelayOnPeriod and grapheme == "." then
				wait(defaultConfigurations.delayTime)
			end
			if defaultConfigurations.extraDelayOnQuesitonMark and grapheme == "?" then
				wait(defaultConfigurations.delayTime)
			end
			if defaultConfigurations.extraDelayOnExclamationPoint and grapheme == "!" then
				wait(defaultConfigurations.delayTime)
			end
			wait(defaultConfigurations.delayTime)
		end
	end
	enterPressed = false
end

Can you please explain your issue another way? I am having trouble understanding.

1 Like

Yeah I don’t understand either

So when I add a print here:

enterPressed = true
print(text)
end

After running through the function 3 times, it prints this:
image
Instead of just printing the third one:
image

For context, here’s after running the first and second time, in that order:
image

image

So I want it to only print the only the “Test text 3”, yet it prints what the text was all other times it was run.