The dialogue for the text overlaps (one npc text then after one is there, I dont want that)

When the player talks to one NPC everything is fine. Until you talk to another NPC the text from the last NPC is overlapping with the other one, I don’t want that :sob: .

Examples:

Here is an example of talking to one NPC:

image

Here is an example of taking to anther NPC:

image

image

These are the scripts for the dialogue:

Local script:


--// SERVICES

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")

--// VARIABLES

local plr = players.LocalPlayer
local hrp = plr.Character:WaitForChild("HumanoidRootPart")
local humanoid = plr.Character:WaitForChild("Humanoid")

local Events = replicatedStorage.Events
local SetObjectiveRE = Events.SetObjectiveEvent

local Map = workspace.Map
local NPCs = Map.NPCs
local NPC1 = NPCs.NPC1
local NPC2 = NPCs.NPC2
local NPC3 = NPCs.Fisher

local NPC1Prompt = NPC1.Torso.DialoguePrompt
local NPC2Prompt = NPC2.Torso.DialoguePrompt
local NPC3Prompt = NPC3.Torso.DialoguePrompt

local MainFrame = script.Parent.MainFrame
local Button1 = MainFrame.Button1
local Button2 = MainFrame.Button2

local dialogueMod = require(script:WaitForChild("Dialogue"))

local Triggered1 = false
local Triggered2 = false
local triggered3 = false

local completedObjective1 = false
local completedObjective2 = false

--// CODE

runService.Heartbeat:Connect(function()
	
	if Triggered1 then

		Button1.TextButton.MouseButton1Click:Connect(function()

			dialogueMod.CloseButton()
			dialogueMod.Activated("Hello!", plr.DisplayName, 0.1)
			dialogueMod.Activated("Hello, "..plr.DisplayName.."!", "NPC", 0.1)
			dialogueMod.Activated("You will go through universes collecting artifacts throughout this adventure of a lifetime..", "NPC", 0.05)
			dialogueMod.Activated("This universe is called 'Test Place' it is the very first universe you will explore!", "NPC", 0.05)
			dialogueMod.Activated("Also make sure you grab every coin in each room!", "NPC", 0.08)
			dialogueMod.Activated("Rise for this expedition!", "NPC", 0.1)
			dialogueMod.closeDialogue()
			humanoid.WalkSpeed = 16
			hrp.Anchored = false 
			
			if completedObjective1 == false then
				completedObjective1 = true
				SetObjectiveRE:FireServer("1. ???")
			end

			task.wait(1)

			SetObjectiveRE:FireServer("1. Make sure to collect coins!")

			task.wait(2)

			NPC1Prompt.Enabled = true 

			Triggered1 = false
		end)

		Button2.TextButton.MouseButton1Click:Connect(function()
			dialogueMod.CloseButton()
			dialogueMod.Activated("Goodbye!", plr.DisplayName, 0.1)
			dialogueMod.Activated("Goodbye..", "NPC", 0.1)
			dialogueMod.closeDialogue()
			task.wait(0.5)
			
			humanoid.WalkSpeed = 16
			hrp.Anchored = false 
			
			if completedObjective1 == false then
				completedObjective1 = true
				SetObjectiveRE:FireServer("1. ???")
			end

			task.wait(1)
			
			SetObjectiveRE:FireServer("1. Make sure to collect coins!")
			
			task.wait(2)

			NPC1Prompt.Enabled = true 

			Triggered1 = false
		end)
	end
	
	if Triggered2 then
		
		Button1.TextButton.MouseButton1Click:Connect(function()
			dialogueMod.CloseButton()
			dialogueMod.Activated("Hey there!", plr.DisplayName, 0.1)
			dialogueMod.Activated("Hello, "..plr.DisplayName.."!", "NPC", 0.1)
			dialogueMod.Activated("Sorry about the mess..", "NPC", 0.05)
			dialogueMod.Activated("What are you doing here?", "NPC", 0.1)
			dialogueMod.Activated("I dont think your supposed to be here..", "NPC", 0.1)
			dialogueMod.Activated("Anyway, I have this special gem for you!", "NPC", 0.1)
			dialogueMod.closeDialogue()

			humanoid.WalkSpeed = 16
			hrp.Anchored = false 
			
			if completedObjective2 == false then
				completedObjective2 = true
				SetObjectiveRE:FireServer("1. ???")
			end

			task.wait(3)

			NPC2Prompt.Enabled = true 
			Triggered2 = false
		end)

		Button2.TextButton.MouseButton1Click:Connect(function()
			dialogueMod.CloseButton()
			dialogueMod.Activated("Farewell..", plr.DisplayName, 0.1)
			dialogueMod.Activated("bye.. :(", "NPC", 0.1)
			dialogueMod.closeDialogue()

			task.wait(0.5)
			
			humanoid.WalkSpeed = 16
			hrp.Anchored = false 
			
			if completedObjective2 == false then
				completedObjective2 = true
				SetObjectiveRE:FireServer("1. ???")
			end

			task.wait(3)

			NPC2Prompt.Enabled = true 
			Triggered2 = false
		end)
	end
	
	if triggered3 then
		
		Button1.TextButton.MouseButton1Click:Connect(function()
			dialogueMod.CloseButton()
			dialogueMod.Activated("Hey", plr.DisplayName, 0.1)
			dialogueMod.Activated("Welcome, I am the fisher!", "Fisher", 0.1)
			dialogueMod.Activated("You can sell the fish to me", "Fisher", 0.1)
			dialogueMod.Activated("Talk to me with the fish in your hand to sell me the fish!", "Fisher", 0.1)
			dialogueMod.closeDialogue()
			
			humanoid.WalkSpeed = 16
			hrp.Anchored = false 
			
			
			task.wait(3)
			
			NPC3Prompt.Enabled = true 
			triggered3 = false
		end)
		
		Button2.TextButton.MouseButton1Click:Connect(function()
			dialogueMod.CloseButton()
			dialogueMod.Activated("Goodbye", plr.DisplayName, 0.1)
			dialogueMod.Activated("See you soon..!", "Fisher", 0.1)
			dialogueMod.closeDialogue()
			
			humanoid.WalkSpeed = 16
			hrp.Anchored = false 

			task.wait(3)

			NPC3Prompt.Enabled = true 
			triggered3 = false
		end)
		
	end
	
end)

NPC1Prompt.Triggered:Connect(function(player)
	Triggered1 = true
	
	if completedObjective1 == false then
		completedObjective1 = true
		SetObjectiveRE:FireServer("Completed..")
	end
	
	NPC1Prompt.Enabled = false
	hrp.Anchored = true 
	humanoid.WalkSpeed = 0
	
	dialogueMod.Activated("...", "NPC", 0.1)
	dialogueMod.Button("Hello!", "Goodbye..")
end)

NPC2Prompt.Triggered:Connect(function(player)
	Triggered2 = true
	
	if completedObjective2 == false then
		completedObjective2 = true
		SetObjectiveRE:FireServer("Completed..")
	end
	
	NPC2Prompt.Enabled = false
	hrp.Anchored = true
	humanoid.WalkSpeed = 0
	
	dialogueMod.Activated("...", "NPC", 0.1)
	dialogueMod.Button("Hey there!", "Farewell..")
end)

NPC3Prompt.Triggered:Connect(function(player)
	triggered3 = true
	
	NPC3Prompt.Enabled = false
	hrp.Anchored = true
	humanoid.WalkSpeed = 0
	
	dialogueMod.Activated("...", "Fisher", 0.1)
	dialogueMod.Button("Hey", "Goodbye..")
end)```

Module script:

```local ts = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)

local DialogueGui = script.Parent.Parent
local DialogueBox = DialogueGui:WaitForChild("MainFrame")
local DialogueText = DialogueBox:WaitForChild("TextBox")
local DialogueName = DialogueBox:WaitForChild("NameBox")
local DialogueButton1 = DialogueBox:WaitForChild("Button1")
local DialogueButton2 = DialogueBox:WaitForChild("Button2")

local sound = script.Parent.Talk

local dialogue = {}

local function writeText(text: string, name: string, interval: number)
	DialogueName.TextLabel.Text = name
	-- DialogueText.TextLabel.MaxVisibleGraphemes = 0
	DialogueText.TextLabel.Text = ""
	
	for i = 1,#text,1 do
		DialogueText.TextLabel.Text = string.sub(text,1,i)
		task.wait(interval)
	end

	--for i = 1, utf8.len(text) do
	--	if sound and sound:IsA("Sound") then sound:Play() end
	--	-- DialogueText.TextLabel.MaxVisibleGraphemes += 1
	--	DialogueText.TextLabel.Text = string.sub(text,1, i)
	--	task.wait(interval)
	--end
end

local function removeText()
	DialogueText.TextLabel.Text = ""
end

local function openDialogue()
	DialogueBox.Visible = true 
	DialogueText.TextLabel.Text = ""
	ts:Create(DialogueBox, tweenInfo, {Position = UDim2.new(0.5, 0, 0.825, 0)}):Play()
	task.wait(1)
end

function dialogue.closeDialogue() 
	ts:Create(DialogueBox, tweenInfo, {Position = UDim2.new(0.5, 0, 1.25, 0)}):Play()
	task.wait(1)
	DialogueBox.Visible = false
	DialogueText.TextLabel.Text = ""
	DialogueName.TextLabel.Text = ""
	DialogueButton1.TextLabel.Text = ""
	DialogueButton2.TextLabel.Text = ""
	DialogueButton1.Visible = false
	DialogueButton1.Interactable = false
	DialogueButton2.Visible = false
	DialogueButton2.Interactable = false
end

function dialogue.Button(buttontxt1, buttontxt2)
	DialogueButton1.Visible = true
	DialogueButton1.Interactable = true
	DialogueButton2.Visible = true
	DialogueButton2.Interactable = true
	
	DialogueButton1.TextLabel.Text = buttontxt1
	DialogueButton2.TextLabel.Text = buttontxt2
end

function dialogue.CloseButton()
	DialogueButton1.Visible = false
	DialogueButton1.Interactable = false
	DialogueButton2.Visible = false
	DialogueButton2.Interactable = false
	
	DialogueButton1.TextLabel.Text = ""
	DialogueButton2.TextLabel.Text = ""
end

function dialogue.Activated(text, name, interval)
	openDialogue()
	writeText(text, name, interval)
	task.wait(1)
end

return dialogue```

Please help :sob:

I dont know why it wont let me send the module script but here it is (probably too much but ok) :


local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)

local DialogueGui = script.Parent.Parent
local DialogueBox = DialogueGui:WaitForChild("MainFrame")
local DialogueText = DialogueBox:WaitForChild("TextBox")
local DialogueName = DialogueBox:WaitForChild("NameBox")
local DialogueButton1 = DialogueBox:WaitForChild("Button1")
local DialogueButton2 = DialogueBox:WaitForChild("Button2")

local sound = script.Parent.Talk

local dialogue = {}

local function writeText(text: string, name: string, interval: number)
	DialogueName.TextLabel.Text = name
	-- DialogueText.TextLabel.MaxVisibleGraphemes = 0
	DialogueText.TextLabel.Text = ""
	
	for i = 1,#text,1 do
		DialogueText.TextLabel.Text = string.sub(text,1,i)
		task.wait(interval)
	end

	--for i = 1, utf8.len(text) do
	--	if sound and sound:IsA("Sound") then sound:Play() end
	--	-- DialogueText.TextLabel.MaxVisibleGraphemes += 1
	--	DialogueText.TextLabel.Text = string.sub(text,1, i)
	--	task.wait(interval)
	--end
end

local function removeText()
	DialogueText.TextLabel.Text = ""
end

local function openDialogue()
	DialogueBox.Visible = true 
	DialogueText.TextLabel.Text = ""
	ts:Create(DialogueBox, tweenInfo, {Position = UDim2.new(0.5, 0, 0.825, 0)}):Play()
	task.wait(1)
end

function dialogue.closeDialogue() 
	ts:Create(DialogueBox, tweenInfo, {Position = UDim2.new(0.5, 0, 1.25, 0)}):Play()
	task.wait(1)
	DialogueBox.Visible = false
	DialogueText.TextLabel.Text = ""
	DialogueName.TextLabel.Text = ""
	DialogueButton1.TextLabel.Text = ""
	DialogueButton2.TextLabel.Text = ""
	DialogueButton1.Visible = false
	DialogueButton1.Interactable = false
	DialogueButton2.Visible = false
	DialogueButton2.Interactable = false
end

function dialogue.Button(buttontxt1, buttontxt2)
	DialogueButton1.Visible = true
	DialogueButton1.Interactable = true
	DialogueButton2.Visible = true
	DialogueButton2.Interactable = true
	
	DialogueButton1.TextLabel.Text = buttontxt1
	DialogueButton2.TextLabel.Text = buttontxt2
end

function dialogue.CloseButton()
	DialogueButton1.Visible = false
	DialogueButton1.Interactable = false
	DialogueButton2.Visible = false
	DialogueButton2.Interactable = false
	
	DialogueButton1.TextLabel.Text = ""
	DialogueButton2.TextLabel.Text = ""
end

function dialogue.Activated(text, name, interval)
	openDialogue()
	writeText(text, name, interval)
	task.wait(1)
end

return dialogue```

Oh my… where do i start with this…

Firstly; you use a heartbeat connection and then connect button click events, that can’t go well.
You also make new variables for each NPC dialogue, eventually your script will be a thousand lines of dialogue alone.

To fix the variable problem,
You could use Collection Service in order to tag every prompt and connect to that
But i’d prefer to use proximity prompt service instead and only have 1 connection

Now, instead of having every dialog in one place, you will create a module script inside of the proximity prompts, name it "DialogModule’

Now, in our local script inside of starter player scripts we write this:

local proximityPromptService = game:GetService("ProximityPromptService")
local dialogueMod = require(script:WaitForChild("Dialogue"))
proximityPromptService.PromptTriggered:Connect(function(prompt:ProximityPrompt)
	local dialog = prompt:FindFirstChild("DialogModule")
	if dialog then
		require(dialog)(dialogueMod)
	end
end)

This will check for the dialog under any prompt that you trigger and run the function that we will return from it with the dialogueMod as parameter

You structure the module as this:

local plr = game:GetService("Players").LocalPlayer
return {
	start = function(dialogueMod)
		dialogueMod.CloseButton()
		dialogueMod.Activated("Hey there!", plr.DisplayName, 0.1)
		dialogueMod.Activated("Hello, "..plr.DisplayName.."!", "NPC", 0.1)
		dialogueMod.Activated("Sorry about the mess..", "NPC", 0.05)
		dialogueMod.Activated("What are you doing here?", "NPC", 0.1)
		dialogueMod.Activated("I dont think your supposed to be here..", "NPC", 0.1)
		dialogueMod.Activated("Anyway, I have this special gem for you!", "NPC", 0.1)
		dialogueMod.closeDialogue()
		
		--[[other necessery code ...]]
	end,
	
	goodbye = function(dialogueMod)
		dialogueMod.CloseButton()
		dialogueMod.Activated("Farewell..", plr.DisplayName, 0.1)
		dialogueMod.Activated("bye.. :(", "NPC", 0.1)
		dialogueMod.closeDialogue()
		
		--[[other necessery code ...]]
	end,
}

We will also update our local script to this:

local proximityPromptService = game:GetService("ProximityPromptService")
local dialogueMod = require(script:WaitForChild("Dialogue"))

local currentDialog

proximityPromptService.PromptTriggered:Connect(function(prompt:ProximityPrompt)
	local dialog = prompt:FindFirstChild("DialogModule")
	if dialog then
		currentDialog = require(dialog)
	end
end)

local MainFrame = script.Parent.MainFrame
local Button1 = MainFrame.Button1.TextButton
local Button2 = MainFrame.Button2.TextButton

Button1.MouseButton1Click:Connect(function()
	if currentDialog then
		currentDialog.start(dialogueMod)
	end
end)

Button2.MouseButton1Click:Connect(function()
	if currentDialog then
		currentDialog.goodbye()
	end
end)

*this is what i assume you what

(post deleted by author)

wow what a nice chat gpt response that actually adresses no issues!

Hey @Hzodx, my intention was to point out some possible adjustments to avoid his overlapping dialogues, if you encountered the issue, I will delete my improvised code so that we don’t confuse the author. Happy you could help.

Sorry for the late response but how would I add text before pressing any button (it’s just a bit “very” confusing for me :sob:)

This is what I have for the module in the prompt:


local completedObjective1 = false
local Triggered1 = false

return {
	start = function(dialogueMod)
		dialogueMod.CloseButton()
		dialogueMod.Activated("Hello!", plr.DisplayName, 0.1)
		dialogueMod.Activated("Hello, "..plr.DisplayName.."!", "NPC", 0.1)
		dialogueMod.Activated("You will go through universes collecting artifacts throughout this adventure of a lifetime..", "NPC", 0.05)
		dialogueMod.Activated("This universe is called 'Test Place' it is the very first universe you will explore!", "NPC", 0.05)
		dialogueMod.Activated("Also make sure you grab every coin in each room!", "NPC", 0.08)
		dialogueMod.Activated("Rise for this expedition!", "NPC", 0.1)
		dialogueMod.closeDialogue()
		humanoid.WalkSpeed = 16
		hrp.Anchored = false 

		if completedObjective1 == false then
			completedObjective1 = true
			SetObjectiveRE:FireServer("1. ???")
		end

		task.wait(1)

		SetObjectiveRE:FireServer("1. Make sure to collect coins!")

		task.wait(2)

		NPC1Prompt.Enabled = true 

		Triggered1 = false
		
	end,

	goodbye = function(dialogueMod)
		dialogueMod.CloseButton()
		dialogueMod.Activated("Goodbye!", plr.DisplayName, 0.1)
		dialogueMod.Activated("Goodbye..", "NPC", 0.1)
		dialogueMod.closeDialogue()
		task.wait(0.5)

		humanoid.WalkSpeed = 16
		hrp.Anchored = false 

		if completedObjective1 == false then
			completedObjective1 = true
			SetObjectiveRE:FireServer("1. ???")
		end

		task.wait(1)

		SetObjectiveRE:FireServer("1. Make sure to collect coins!")

		task.wait(2)

		NPC1Prompt.Enabled = true 

		Triggered1 = false
	end,
}```

Sorry if I’m impatient but do you know how to set it up fully

I just don’t get what it means, the message about explains it more