Dialog system logic error

Ok, so I am attempting to create a dialog system that takes conversations written from a module script and converts them into GUI text. The script works for the first NPC it is used on but if the dialog is run through multiple times or on a different NPC it doesn’t work properly

local gui = script.Parent
local frm = gui.Frame
local Option1 = frm.Option1
local Option2 = frm.Option2
local Nametag = frm.NameTag
local bodyText = frm.BodyText
local remotes = game:GetService("ReplicatedStorage").Remotes
local RunService = game:GetService("RunService")
local heartbeat = RunService.RenderStepped
isplaying = false
local function changeText(count, plrText, NpcText)
	isplaying = true
	Option1.Text = plrText[count][1]
	Option2.Text = plrText[count][2]
	local goal = NpcText[count]
	local timeToTake = #goal/30
	local accumlated = 0
	while timeToTake > accumlated do
		accumlated += heartbeat:Wait()
		bodyText.Text = string.sub(goal,1,math.floor((accumlated/timeToTake)*#goal))
	end
	isplaying = false
end

remotes.DialogRe.OnClientEvent:Connect(function(dialogTable, NPC_NAME, interactionType)
	if isplaying == false then
		isplaying = true
		local count = 1 
		print(count)
		frm:TweenSize(UDim2.new(0.7, 0,0.15, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0.2)
		Nametag.Text = NPC_NAME
		gui.Enabled = true
		local NPCtext = dialogTable.NPC
		local PLAYERtext = dialogTable.PLAYER
		changeText(count, PLAYERtext, NPCtext)
		Option1.Activated:Connect(function()
			if isplaying == false and count ~= #NPCtext then
				count += 1
				changeText(count, PLAYERtext, NPCtext)
				print(count)
				return
			elseif isplaying == false and count == #NPCtext then
				gui.Enabled = false
				print(count)
				return
			end	

		end)
		Option2.Activated:Connect(function()
			if isplaying == false then
				frm:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0.2)
				count = 1
				gui.Enabled = false

			end
		end)
	end
end)

image

image

As you can see, when the script is run for a conversation with only 1 line, it somehow prints the count variable as 3. When the conversation is run twice in a row, it also prints the final count multiple times. I am confused about what to do and would appreciate any feedback.

Option1.Activated:Connect(function()
			if isplaying == false and count ~= #NPCtext then
				count += 1
				changeText(count, PLAYERtext, NPCtext)
				print(count)
				return
			elseif isplaying == false and count == #NPCtext then
				gui.Enabled = false
				print(count)
				return
			end	

		end)
		Option2.Activated:Connect(function()
			if isplaying == false then
				frm:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0.2)
				count = 1
				gui.Enabled = false

			end
		end)

These connections are being created each time the outer OnClientEvent event/signal is fired, you need to disconnect these connections such that future connections don’t stack ontop of existing connections.

local Conn1
Conn1 = Option1.Activated:Connect(function()
	Conn1:Disconnect()
	if isplaying == false and count ~= #NPCtext then
		count += 1
		changeText(count, PLAYERtext, NPCtext)
		print(count)
		return
	elseif isplaying == false and count == #NPCtext then
		gui.Enabled = false
		print(count)
		return
	end	

end)
local Conn2
Conn2 = Option2.Activated:Connect(function()
	Conn2:Disconnect()
	if isplaying == false then
		frm:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0.2)
		count = 1
		gui.Enabled = false

	end
end)