Problem with dialogue system

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a working Dialogue system, but I am having trouble

  2. What is the issue? Include screenshots / videos if possible!
    It seems to work fine, but when I try to press the “pos vale” button this happens:


    (I only click one)
    It works weird, the order is just not okay. Keep reading and you will see what it is supposed to look like.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Chatgpt
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

For this project, I’m using Defaultio’s rich text, which is not the problem because it just works fine.
(Defaultio’s rich text download just in case you want to get it: https://create.roblox.com/marketplace/asset/1014847041/Rich-Text-Markup)

This is the main script:

local player = game.Players.LocalPlayer
local tweenService = game:GetService("TweenService")
local DialogueEvent = game.ReplicatedStorage.Events.DialogueEvent
local richText = require(script:WaitForChild("RichText"))

DialogueEvent.OnClientEvent:Connect(function(NPC)
	local prompt = NPC.HumanoidRootPart:FindFirstChildOfClass("ProximityPrompt")
	local textSequence = require(prompt.TextSequence)
	
	
	
	local character = player.Character or player.CharacterAdded:Wait()
	local rootPart = character:WaitForChild("HumanoidRootPart")

	--[[prompt.Parent.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position,
		Vector3.new(rootPart.Position.X, prompt.Parent.Position.Y, rootPart.Position.Z))]]


	local tweenInfo = TweenInfo.new(
		.5,
		Enum.EasingStyle.Exponential,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)

	local tween = tweenService:Create(prompt.Parent, tweenInfo, {CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position,
		Vector3.new(rootPart.Position.X, prompt.Parent.Position.Y, rootPart.Position.Z))})
	tween:Play()
	
	local infos = {
		Head = TweenInfo.new(
			.5,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		),
		Back = TweenInfo.new(
			.5,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		),
		Title = TweenInfo.new(
			.5,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		),
		Text = TweenInfo.new(
			.5,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		),
	}
	
	for i, v in pairs(infos) do
		
		if i == 1 then
			v.time = .2
		elseif i == 2 then
			v.time = .3
		elseif i == 3 then
			v.time = .1
		elseif i == 4 then
			v.time = .15
		end
		
		
	end
	
	
	local metas = {
		Head = UDim2.new(0.072, 0,0.592, 0),
		Back = UDim2.new(0.298, 0,0.592, 0),
		Title = UDim2.new(0.307, 0,0.62, 0),
		Text = UDim2.new(0.35, 0,0.7, 0)
	}
	
	
	local UI = script.UIDialoge:Clone()
	
	UI.Parent = player.PlayerGui
	
	NPC.Archivable = true
	
	local inView = NPC:Clone()
	
	spawn(function()
		
		for i,v in pairs(inView:GetChildren()) do
			
			if v:IsA("Part") then
				
				v.Anchored = true
				
			end
			
		end
		
	end)
	
	
	inView:SetPrimaryPartCFrame(CFrame.new(0.630999923, 0.102999523, -3.65199995, -0.913545489, 2.04631364e-12, -0.406736523, 7.27595761e-12, 0.999999881, -2.13731255e-11, 0.406736583, 2.24847207e-11, -0.91354537))
	
	
	inView.Parent = UI.Head.ViewportFrame
	
	local Camera = Instance.new("Camera")
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = CFrame.new(0.625999987, 1.38499999, -1.73300004, 1, 0, 0, 0, 1, 0, 0, 0, 1)
	Camera.Parent = inView.Parent
	
	UI.Head.ViewportFrame.CurrentCamera = Camera
	
	spawn(function()
		
		for i,v in pairs(inView:GetChildren()) do
			if v:IsA("Part") then
				v.CanCollide = false
				v.Massless = true
			end

			
		end
		
	end)
	
	
	
	
	
	for i, UIobject in pairs(UI:GetChildren()) do
		
		if UIobject.Name == "Head" then
			
			local tween = tweenService:Create(UIobject, infos.Head, {Position = metas.Head})
			tween:Play()
			
			local tween2 = tweenService:Create(UIobject, infos.Head, {Transparency = .3})
			tween2:Play()
			
		elseif UIobject.Name == "Back" then
			
			local tween = tweenService:Create(UIobject, infos.Back, {Position = metas.Back})
			tween:Play()
			
			local tween2 = tweenService:Create(UIobject, infos.Back, {ImageTransparency = .3})
			tween2:Play()
			
			local tween3 = tweenService:Create(UIobject, infos.Back, {Transparency = 0})
			tween2:Play()
			
		elseif UIobject.Name == "Title" then
			
			UIobject.Text = NPC.Name
			
			local tween = tweenService:Create(UIobject, infos.Title, {Position = metas.Title})
			tween:Play()
			
		elseif UIobject.Name == "Text" then
			local tween = tweenService:Create(UIobject, infos.Text,{Position = metas.Text})
			tween:Play()
			tween.Completed:Connect(function()
					
					local function showDialogue(text, frame, dela)
						local textObject = richText:New(UIobject, text)--, AnimateStyle = "Wiggle", AnimateStepFrequency = 1, AnimateStyleTime = 7, AnimateStyleNumPeriods = 10})
					textObject:Animate(true)
					task.wait(dela)
					end
					
					local function hideDialogue()	
						for i, v in pairs(UIobject:GetChildren()) do
							if not v:IsA("Script") and not v:IsA("LocalScript") and not v:IsA("ModuleScript") then
								v.Parent = nil
							end
						end
					end
					showDialogue(textSequence[1].Text, UIobject, textSequence[1].Delay)

					local function closeEverything()

						for i,v in pairs(UI:GetChildren()) do
							v.Parent = nil
						end
					
					DialogueEvent:FireServer()
					
					end

					local AnswerHolder = UI.AnswerHolder
					local AbleToResponse = false

					local function showAnswers(answer, bye)
						AbleToResponse = true
						for i,v in pairs(AnswerHolder:GetChildren()) do
							local tween = tweenService:Create(v, TweenInfo.new(.2), {TextTransparency = 0})
							tween:Play()

							if v.Name == "Goodbye" then
								if bye then
									v.Text = bye
								else
									v.Text = "Goodbye"
								end
							elseif v.Name == "Answer" then
								if answer then
									v.Text = answer
								else
									v.Text = "..."
								end

							end

						end
					end
					local function hideAnswers()
						AbleToResponse = false
						for i,v in pairs(AnswerHolder:GetChildren()) do
							local tween = tweenService:Create(v, TweenInfo.new(.2), {TextTransparency = 1})
							tween:Play()
						end
					end
					
					
					
				showAnswers(textSequence[1].Answer, textSequence[1].Goodbye)
				local CloseMe = false
					for i,AnswerButton in pairs(AnswerHolder:GetChildren()) do

						AnswerButton.InputBegan:Connect(function(input)

							if input.UserInputType == Enum.UserInputType.MouseButton1 then
								if AbleToResponse then
									if AnswerButton.Name == "Answer" then
									hideAnswers()
									
									print(CloseMe)
									if CloseMe == true then
										closeEverything()
									end
									

									
									for indice = 2, #textSequence do
										print(indice)
										
										if next(textSequence, indice) == nil then

											
											CloseMe = true
											print("close please")
											
										end
										
										showDialogue(textSequence[indice].Text, UIobject, textSequence[indice].Delay)
										showAnswers(textSequence[indice].Answer, textSequence[indice].Goodbye)
									end
									elseif AnswerButton.Name == "Goodbye" then
									hideAnswers()
									hideDialogue()
									showDialogue(". . .", UIobject, 1)
									closeEverything()

									end
								end
							end	
						end)
					end
					

				

				

			end)
		end
	end
	
	
	
	
end)

and the code of textSequence:

local textSequence = {
	{Text = "This is the text", Delay = 0, Goodbye = "See ya", Answer = "pos vale"},
	{Text = "This is the second text", Delay = 1},
	{Text = "third", Delay = 1},
	{Text = "number fooour", Delay = 1},
	{Text = "number five", Delay = 1}
}

return textSequence

and this is the proximity prompt script:


local prompt = script.Parent
local NPC = script.Parent.Parent.Parent
NPC.Archivable = true

local tweenService = game:GetService("TweenService")
local DialogueEvent = game.ReplicatedStorage.Events.DialogueEvent

local SpeakingWith = {}


prompt.Triggered:Connect(function(player)
	if not table.find(SpeakingWith, player) then
		
		pcall(function()table.insert(SpeakingWith, player.UserId) end)
		
		DialogueEvent:FireClient(player, NPC)
		
	end
end)

DialogueEvent.OnServerEvent:Connect(function(player, what)
	pcall(function()table.remove(SpeakingWith, player.UserId) end)
	
end)

I think the problem is in the last for loop, if that can help.
Also, this is my explorer:
image
the npc

image
the ui
Thank you so much.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like