How can I make a function wait before it starts running again?

Hi! I need help with my codes. I’m trying to make a dialogue for my game and it’s working fine. When I press the A button on the keyboard, the dialogue will continue on being displayed until the end of the line (kind of like the ones in Pokemon). The problem is that when I press the A button really fast, it won’t stop before heading on to the next lines of the dialogue. I’ve already tried yield but it won’t work. Here are my codes:

#pragma strict

import System;
import System.IO;
import System.Text;

var skin : GUISkin;
private var path : String = "C:/Users/quevin/Documents/Goopi Club/Goopi Club/Assets/Text Files/New Game.txt";
private var strFile : String;
private var lines : String [];
private var txtDialogue : String = "";
private var delay : float = 0.005;
private var linePos : int = 0;
private var i : int;
private var j : int;
private var checkPunct = false;

function Start()
{
	var reader : StreamReader = new File.OpenText(path);
	strFile = reader.ReadToEnd();
	reader.Close();
	
	lines = strFile.Split("

"[0]);
TextDialogue2();
}

function Update()
{
	if(Input.GetKeyDown(KeyCode.A))
	{
		if(linePos != lines.Length) TextDialogue2();
		else print("All lines have been printed.");
	}
}

function OnGUI()
{
	GUI.skin = skin;
	GUI.BeginGroup(Rect(20, 10, 700, 580));
		
		GUI.BeginGroup(Rect(0, 455, 700, 125));
			
			GUI.Box(Rect(0, 0, 700, 125), "");
			GUI.Label(Rect(120, 35, 685, 110), txtDialogue);

		GUI.EndGroup();
	
	GUI.EndGroup();
	

}

function TextDialogue()
{
	txtDialogue = "";
	checkPunct = false;
	for(i = linePos; i < (linePos + 2); i++)
	{
		for(j = 0; j < lines*.Length; j++)*
  •  {*
    

_ txtDialogue = txtDialogue + lines*.Substring(j, 1);_
_
yield WaitForSeconds(delay);_
if(lines.Substring(lines.Length - 2, 1).Equals(“.”) || lines.Substring(lines.Length - 2, 1).Equals(“!”) || lines_.Substring(lines.Length - 2, 1).Equals(“?”)) checkPunct = true;
}*_

* if(checkPunct == true)*
* {*
* i = i + 1;*
* break;*
* }*

* txtDialogue = txtDialogue + "
";*

* }*

* linePos = i;*
}

function TextDialogue2()
{
* yield TextDialogue();*
}

Just use a boolean that allows / disallows interaction based on if the line is finished typing. Tested this and works like a charm on my end.

#pragma strict

import System;
import System.IO;
import System.Text;

var skin : GUISkin;
private var path : String = "C:/Users/quevin/Documents/Goopi Club/Goopi Club/Assets/Text Files/New Game.txt";
private var strFile : String;
private var lines : String [];
private var txtDialogue : String = "";
private var delay : float = 0.005;
private var linePos : int = 0;
private var i : int;
private var j : int;
private var checkPunct = false;
private var autotype : boolean = false;

function Start()
{
	var reader : StreamReader = new File.OpenText(path);
	strFile = reader.ReadToEnd();
	reader.Close();
	
	lines = strFile.Split("

"[0]);
TextDialogue2();
}

function Update()
{
	if(Input.GetKeyDown(KeyCode.A))
	{
		//Only allow interaction when autotype is true.
		if(autotype)
		{
			if(linePos != lines.Length) TextDialogue2();
			else print("All lines have been printed.");
		}
	}
}

function OnGUI()
{
	GUI.skin = skin;
	GUI.BeginGroup(Rect(20, 10, 700, 580));
	GUI.BeginGroup(Rect(0, 455, 700, 125));
	GUI.Box(Rect(0, 0, 700, 125), "");
	GUI.Label(Rect(120, 35, 685, 110), txtDialogue);
	GUI.EndGroup();
	GUI.EndGroup();
}

function TextDialogue()
{
	txtDialogue = "";
	checkPunct = false;
	for(i = linePos; i < (linePos + 1); i++)
	{
		for(j = 0; j < lines*.Length; j++)*
  •  {*
    

_ txtDialogue = txtDialogue + lines*.Substring(j, 1);*_

* //Enable and Disable autotype based on if the line is finished typing or not.*
_ if(j == lines*.ToCharArray().Length - 1)
autotype = true;
else*
* autotype = false;
//*_

* yield WaitForSeconds(delay);*
if(lines_.Substring(lines*.Length - 2, 1).Equals(“.”)
|| lines.Substring(lines.Length - 2, 1).Equals(“!”)
|| lines.Substring(lines.Length - 2, 1).Equals(“?”)) checkPunct = true;
}*_

* if(checkPunct == true)*
* {*
* i = i + 1;*
* break;*
* }*

* txtDialogue = txtDialogue + "
";*

* }*
* linePos = i;*
}

function TextDialogue2()
{
* yield TextDialogue();*
}