Dialogue starts over for no reason

I’ve been working on a custom NPC dialogue but I’ve ran into an issue. For whatever reason, randomly it will start the dialogue back over to the start (sometimes even more than once). Im unsure on what could be causing this and have speculated for hours but to no prevail. Any help is very much appreciated!!!

I can provide more information if needed!

Local Script:

NOTE: The Dialogue Variable is a module script storing important info, such as the choices, the next line of dialogue, and etc.

local function NextDialogue(Dialogue, NPC)
        if CurrentDialogue.Choices then --Checks if the current dialogue has choices
        for i, v in pairs(CurrentDialogue.Choices) do --Goes through each choice
            --Creates each and every choice button!
            local ClonedTemplate = Template:Clone() 
            ClonedTemplate.Name = i
            ClonedTemplate.Text = v.Text
            ClonedTemplate.Parent = ChoicesFrame
            
            TS:Create(ClonedTemplate, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 0, TextTransparency = 0}):Play()
            
            wait(.1)
            
            ClonedTemplate.Activated:Connect(function()
                Current = CurrentDialogue.Choices[tonumber(ClonedTemplate.Name)].Next
                
                ChatEvent:FireServer(player.Character, v.Text)
                NextDialogue(Dialogue, NPC)
            end)
        end
end

if CurrentDialogue.Next then
        LeaveButton:Destroy()
        
        NextButton.Visible = true
        ClickFadeIn:Play()
        
        NextButton.Activated:Connect(function()
            NextButton.Visible = false
            Frame.BackgroundColor3 = ShadedColor
            ClickFadeOut:Play()
            
            --print(Current, CurrentDialogue)
            
            print("Changing Current to "..CurrentDialogue.Next)
            
            task.wait(.1)
            
            Current = CurrentDialogue.Next
            Frame.BackgroundColor3 = NormalColor
            NextDialogue(Dialogue, NPC)
        end)
    end

Video:

One thing I notice in your NextDialogue function is that it instantiates new instances of Template, which I presume to be dialogue choice buttons.

I can’t see anywhere in your code where you clean up the old choice buttons. My presumption here is that the old dialogue choice buttons are overriding the new ones for the current message in the dialogue maybe?

I think it would be a bit easier to just re-use your dialogue buttons. You could assign a variable with the current state the dialogue is in, and then use that to determine the responses to display on the dialogue buttons.

I left out the part where at the beginning of the NextDialogue function, it clears out any choice templates and deletes them allowing for ones to spawn in. I don’t believe its the buttons that are the issue. It only occurs when the dialogue doesn’t have the choices option and only can continue the dialogue using .Next (i can elaborate further if you need)

1 Like