NPC Dialogue Choices Refuse To Function

so i have this dialogue script, and it used to work fine. i opened studio after a while, and for some reason, if there is no nextNode provided, the dialogue exit does not show up. i’ve tried using ChatGPT, but it has not been helpful, and has only broken my script even further.

before my script broke, if there was no nextNode, it would bring up the exit dialogue button. now it doesn’t do that? i have not touched the code, which is very confusing.

local gui = script.Parent
local container = gui:WaitForChild("choiceContainer")
local choiceTemplate = script:WaitForChild("replyChoice")
local npcText = gui:WaitForChild("npcTextFrame"):WaitForChild("npcText")
local typewriter = require(game:GetService("ReplicatedStorage"):WaitForChild("centralText"))
local departureText = "See you later!"

local dialogueNodes = {
	[1] = {
		text = "Greetings! This is an NPC dialogue test. How are you on this fine evening?",
		choices = {
			[1] = { text = "Not great right now, it could be better", nextNode = 2 },
			[2] = { text = "I'm feeling fantastical! How are you?", nextNode = 3 },
			[3] = { text = "My mood is mediocre", nextNode = 4 },
		}
	};
	[2] = { text = "I'm sorry to hear that..." };
	[3] = {
		text = "I'm feeling refreshed! Is there anything else you would like to know about?",
		choices = {
			[1] = { text = "Fine dining!", nextNode = 5, follow = true },
			[2] = { text = "No! Get off my case, buzzard face!", nextNode = 6, follow = true },
			[3] = { text = "The shopping scene.", nextNode = 7, follow = true },
		}
	};
	[4] = {
		text = "Same, to be honest."
	};
	[5] = {
		text = "Ah! There are many wonderful restaurants around here! My favourite is the one down the street.",
		choices = {}
	};
	[6] = {
		text = "You needn't be rude...",
		choices = {}
	};
	[7] = {
		text = "You are interested in the shopping experiences! Might I suggest the local farmer's market?",
		choices = {
			[1] = { text = "I'll be sure to check it out. Have a nice day!" }, -- now if you click this option, nothing happens
			[2] = { text = "I don't like that farmer's market. I ate some honey from over there, and it made me sick.", nextNode = 2 }
		}
	};
}

local function createDialogue(currentNode)
	-- clear previous choices
	for _, child in ipairs(container:GetChildren()) do
		if child:IsA("TextButton") then
			child:Destroy()
		end
	end

	-- set NPC text
	typewriter:tweenTypewriter(dialogueNodes[currentNode].text, npcText, true, 3, "rbxassetid://8253504731")

	-- create choices if available
	local choices = dialogueNodes[currentNode].choices
	if choices and #choices > 0 then
		for choiceIndex, choiceData in ipairs(choices) do
			local choiceInstance = choiceTemplate:Clone()
			choiceInstance.Name = "Choice" .. choiceIndex
			choiceInstance.Text = choiceData.text
			choiceInstance.Parent = container

			-- connect click event for choices
			choiceInstance.MouseButton1Click:Connect(function()
				local nextNode = choiceData.nextNode
				if nextNode then
					if choiceData.follow then
						createDialogue(nextNode)
					else
						createDialogue(nextNode)
					end
				end
			end)
		end
		-- now this does not function if there is no nextNode? it used ot work just fine.
	elseif choices and not choices.nextNode then
		-- no options left
		local noRepliesLeft = script:WaitForChild("exitButton"):Clone()
		noRepliesLeft.Text = departureText
		noRepliesLeft.Parent = container
		noRepliesLeft.MouseButton1Down:Once(function()
			print("conversation over")
			gui.Enabled = false
		end)
	else
		-- no options left here, either
		local noRepliesLeft = script:WaitForChild("exitButton"):Clone()
		noRepliesLeft.Text = departureText
		noRepliesLeft.Parent = container
		noRepliesLeft.MouseButton1Down:Once(function()
			print("conversation over")
			gui.Enabled = false
		end)
	end
end

-- initialize dialogue with the first node
createDialogue(1)

solved it myself.
all i had to do was sort of break up the code into separate functions.

function createChoiceButton(choiceData, choiceIndex)
	local choiceInstance = choiceTemplate:Clone()
	choiceInstance.Name = "Choice" .. choiceIndex
	choiceInstance.Text = choiceData.text
	choiceInstance.Parent = container

	choiceInstance.MouseButton1Click:Connect(function()
		local nextNode = choiceData.nextNode
		if nextNode then
			createDialogue(nextNode)
		else
			print("conversation over")
			gui.Enabled = false
		end
	end)
end

function createDialogue(currentNode)
	-- clear previous choices
	for _, child in ipairs(container:GetChildren()) do
		if child:IsA("TextButton") then
			child:Destroy()
		end
	end

	-- create NPC text
	typewriter:tweenTypewriter(dialogueNodes[currentNode].text, npcText, true, 3, "rbxassetid://8253504731")

	-- create choices if available
	local choices = dialogueNodes[currentNode].choices
	if choices and #choices > 0 then
		for choiceIndex, choiceData in ipairs(choices) do
			createChoiceButton(choiceData, choiceIndex)
		end
	else
		-- no options left
		local noRepliesLeft = script:WaitForChild("exitButton"):Clone()
		noRepliesLeft.Text = departureText
		noRepliesLeft.Parent = container
		noRepliesLeft.MouseButton1Down:Once(function()
			print("conversation over")
			gui.Enabled = false
		end)
	end
end

createDialogue(1)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.