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)