How i solve this?

Im making this script, where it shows the dialogue frame, shows the text and then closes.

The problem is that when you open 2 dialogues in a very close time, it closes the dialogue frame before it should.

function dialogue(player,text)
	if player == plr then
		gameGUI.DialogueFrame.Visible = true
		gameGUI.DialogueFrame.Text.Text = ""
		for i = 1, #text do
			gameGUI.DialogueFrame.Text.Text = gameGUI.DialogueFrame.Text.Text .. text:sub(i, i)
			game.ReplicatedStorage.Effects.GenericBeepSFX:Play()
			if text:sub(i, i) == "." or text:sub(i, i) == "!" or text:sub(i, i) == "?" then
				wait(0.5)
			elseif text:sub(i, i) == "," then
				wait(0.3)
			end
			wait(0.02)
		end
		wait(2)
		gameGUI.DialogueFrame.Visible = false -- HERE
	end
end

I think you already solved this, you just changed some bits up.

Its not the same. On that case, it duplicates the text, here it closes the dialogue frame before the text has done writing.

Solved. I made a variable to check if the text its still typing.