Dialogue system ends after first line

I’m working on a cutscene + dialogue system using a LocalScript inside StarterGui. I have a DialogueGui with a TextLabel (DialogueText) and a TextButton (NextButton). I defined multiple dialogue lines in a table like this:

local dialogueLines = {
    "sup",
    "this is a test dialogue",
    "k"
}

However, after clicking the Next button once, the dialogue ends immediately, even though there are clearly 3 lines.

What I’ve tried

  • Printing the currentLine value
  • Double checked that the script only uses currentLine += 1 once
  • Ensured the dialogue GUI is only disabled in the else block
  • Re-set currentLine = 1 before starting the dialogue
  • Tried disabling other scripts to avoid conflict
  • there are no errors or warnings

Local script:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local dialogueGui = script.Parent
local dialogueText = dialogueGui:WaitForChild("DialogueText")
local nextButton = dialogueGui:WaitForChild("NextButton")

local mainQuest = game.StarterGui:WaitForChild("QuestGUI"):WaitForChild("MainQuestLabel")
local secondQuest = game.StarterGui:WaitForChild("QuestGUI"):WaitForChild("SecondQuestLabel")

local npc = workspace:WaitForChild("QuestNPC")
local talkTrigger = npc:WaitForChild("TalkTrigger")
local camPart = workspace:WaitForChild("CutsceneCam2")

local dialogueLines = {
	"sup",
	"this is a test dialogue",
	"k"
}
local currentLine = 1
local inDialogue = false

local PlayerModule = require(player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
local controls = PlayerModule:GetControls()

local promptGui = Instance.new("BillboardGui")
promptGui.Size = UDim2.new(0, 100, 0, 50)
promptGui.StudsOffset = Vector3.new(0, 3, 0)
promptGui.Adornee = talkTrigger
promptGui.AlwaysOnTop = true
promptGui.Enabled = false

local promptLabel = Instance.new("TextLabel")
promptLabel.Text = "[E] Talk"
promptLabel.Size = UDim2.new(1, 0, 1, 0)
promptLabel.BackgroundTransparency = 1
promptLabel.TextColor3 = Color3.new(1, 1, 1)
promptLabel.TextScaled = true
promptLabel.Font = Enum.Font.GothamBold
promptLabel.Parent = promptGui

promptGui.Parent = talkTrigger

RunService.RenderStepped:Connect(function()
	if inDialogue then return end
	local dist = (humanoidRootPart.Position - talkTrigger.Position).Magnitude
	promptGui.Enabled = dist <= 10
end)

local function startDialogue()
	inDialogue = true
	currentLine = 1

	controls:Disable()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = camPart.CFrame

	dialogueGui.Enabled = true
	dialogueText.Text = dialogueLines[currentLine]
	nextButton.Visible = true
end

UserInputService.InputBegan:Connect(function(input, processed)
	if processed or inDialogue then return end
	if input.KeyCode == Enum.KeyCode.E then
		local dist = (humanoidRootPart.Position - talkTrigger.Position).Magnitude
		if dist <= 10 then
			startDialogue()
		end
	end
end)

nextButton.MouseButton1Click:Connect(function()
	currentLine += 1
	if currentLine <= #dialogueLines then
		dialogueText.Text = dialogueLines[currentLine]
	else
		dialogueGui.Enabled = false
		camera.CameraType = Enum.CameraType.Custom
		controls:Enable()

		mainQuest.Text = ""
		secondQuest.Visible = false

		inDialogue = false
	end
end)

Can’t figure it out just by looking at the code snippet. Could you add “print(currentLine <= #dialogLines, currentLine, #dialogLines)” to just before the if check in the mousebutton1click connection and screenshot what the output says?

My best guess would be that you have more than 1 dialog script in your dialoggui and one of those scripts only have 1 line, so when you open the gui and click next it tries progressing both dialog trees at the same time, thus disabling the ui. But you said you disabled some scripts already so idk.

2 Likes

I agree with this. Your script that you provided looks logically good, so I would have to assume another script is also connecting to the NextButton.MouseButton1Click and disabling the GUI

the only error I could find in that script is not related to the bug you are encountering. To get a reference to mainQuest and secondQuest, you need to use the PlayerGui and not the StarterGui. PlayerGui represents the Gui that any individual player owns, so that is where the actual references will be. Modifying StarterGui will change how the UI loads on respawn, with no immediate effect.

So change

local mainQuest = game.StarterGui:WaitForChild("QuestGUI"):WaitForChild("MainQuestLabel")
local secondQuest = game.StarterGui:WaitForChild("QuestGUI"):WaitForChild("SecondQuestLabel")

to this:

local PlayerGui = player:WaitForChild("PlayerGui")
local mainQuest = PlayerGui:WaitForChild("QuestGUI"):WaitForChild("MainQuestLabel")
local secondQuest = PlayerGui:WaitForChild("QuestGUI"):WaitForChild("SecondQuestLabel")

And that might also be a clue to what your isssue is? I do not know if connections are also cloned from StarterGui when that happens. Try to make it so that you only have the one single connection to NextButton.MouseButton1Click and see how that works out

2 Likes

ok ill try it, will let you guys know once i tried it :+1:t2: