Hello scripting helpers i have a problem with a custom dialog system. Where you chat with a npc and stuff.

The problem is that when i'm done with a chat with a npc and then start a chat with it again it does not wait for you to click E it just continues for the first message but then goes back to normal in the next message so for example it would first be: "Hello to you" and then it would not wait for you to press E but just go to the next message and for example say: "This is a problem" and then wait for you to press E in the rest of the dialog.

--These two scripts are placed in the startergui

--Module script

local m = {}

local chatui = script.Parent.Parent.Parent:WaitForChild("ChatUi")
local ui = script.Parent.Parent.Parent:WaitForChild("ui") --Some other stuff
local background = chatui:WaitForChild("Background")
local chatfr = background:WaitForChild("Chat")
local chatnamefr = background:WaitForChild("ChatName")
local chatimagefr = background:WaitForChild("ChatImage")
local pressefr = background:WaitForChild("PressE")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local chatting = false

function m.preparechat(chatname,chatimage) --Just setting some things up
	chatui.Enabled = true
	ui.Enabled = false
	chatfr.Text = ""
	chatnamefr.Text = chatname
	chatimagefr.Image = "rbxassetid://"..chatimage
end

function m.settext(text)
	chatfr.Text = text
end

function m.startchat(chats,chatname,chatimage,open) --This is the main script
	if #chats > 0 then
		local currentnumber = 0
		if chatting == false then
			chatting = true
			m.preparechat(chatname,chatimage)
			for a,b in pairs(chats) do --Getting the messages
				pressefr.Visible = false
				keypressed = false
				currentnumber = a
				for i = 1,#b do --A effect with the words where it "types"							 
                                        local text = string.sub(b,1,i)
					m.settext(text)
					if string.sub(b,i,i) == "." or string.sub(b,i,i) == "!" or string.sub(b,i,i) == "?" then
						if i ~= #b then --A wait 
							wait(.2)
						end
					elseif string.sub(b,i,i) == "," then --A shorter wait
						if i ~= #b then
							wait(.05)
						end
					end
					wait()
				end
				pressefr.Visible = true
				local number = a
				mouse.KeyDown:connect(function(key)
					key = string.upper(key)
					if key == "E" then --When you have read the message and what to continue the chat
						if number == currentnumber then --Debug
							keypressed = true
						end
					end
				end)
				repeat wait() until keypressed == true --Waits until you have pressed E
			end
			if open then --Checks if open is a thing
				open.Visible = true
			end
		end
		chatui.Enabled = false
		ui.Enabled = true
		chatting = false
	end
	return true
end

return m

--Local script

local chat = require(script.Parent:WaitForChild("Modules"):WaitForChild("Chat"))
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character or plr.CharacterAdded:Wait()

local range = 20
local chatting = false

local open = script.Parent.Parent:WaitForChild("ui"):WaitForChild("InventoryUi")

local target = workspace:WaitForChild("Dummy")

while true do
	if (target:WaitForChild("HumanoidRootPart").CFrame.p - char.HumanoidRootPart.CFrame.p).magnitude <= range then
		local billboard
		if chatting == false then --Debug
			if not target:WaitForChild("HumanoidRootPart"):FindFirstChildOfClass("BillboardGui") then --A billboard that says E
				billboard = script:WaitForChild("BillboardGui"):Clone()
				billboard.Parent = target:WaitForChild("HumanoidRootPart")
			end
		end
		mouse.KeyDown:connect(function(key) --Checks if the player wants to chat with the target
			if string.upper(key) == "E" then
				if chatting == false then
					chatting = true
					local done = chat.startchat({"Hello i think i know you. But i'm not sure... So i'm just going so think me about","From - Harry.","Your good friend from your home town"} --Chat,"Harry" --Chat name,1650560222 --Chat image,open --And what gui that is going to open)
					repeat wait() until done
					chatting = false
				end
			end
		end)
	else
		if target:WaitForChild("HumanoidRootPart"):FindFirstChildOfClass("BillboardGui") then
			target:WaitForChild("HumanoidRootPart"):FindFirstChildOfClass("BillboardGui"):Destroy()
		end
	end
	wait()
end