Dialogue system not working

For some weird reason the activatedModule variable suddenly switches dialogues with the wrong one whenever I click the continue or leave button (the continue and leave function) I know for a certain that this is only with these 2 functions, as I did print statements on it and it switches dialogues with a different one out of all dialogues. All other functions keep having the right selected activatedModule, so I’m genuinely confused.

Script:

local dialogueManager = {}

-- Main vars
local replicated = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local player = game:GetService("Players").LocalPlayer
local playerUI = player:WaitForChild("PlayerGui")
local sounds = script.Parent:WaitForChild("Sounds")

-- Modules
local modules = replicated:WaitForChild("Modules")
local infoHandler = modules.DialogueManager
local dialogueInfoModules = infoHandler.Dialogues:GetChildren()
local typewriteModule = require(modules.TypewriteModule)

-- Second vars
local dialogueUI = playerUI:WaitForChild("DialogueGUI")
local nameLabel = dialogueUI.MainFrame.NPCName
local dialogueText = dialogueUI.MainFrame.DialogueText
local yesButton = dialogueUI.YesButton
local noButton = dialogueUI.NoButton

-- Setting Variables
local dialogueIndex = "Option1"
local nextDialogue = nil
local typing = false
local activated = false
local activatedModule

-- Tweeninfo's
local mainFrameTweenInfo = TweenInfo.new(
	.25,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out
)

local buttonsTweenInfo = TweenInfo.new(
	.3,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut
)

-- Local Functions
local function clearDialogue()
	nameLabel.Text = ""
	dialogueText.Text = ""
	yesButton.Text = ""
	noButton.Text = ""
	dialogueIndex = "Option1"
	activated = false
	yesButton.Visible = true
	noButton.Visible = true
end

local function updateText(dialogueOptions)
	if typing then return end
	typing = true

	typewriteModule(dialogueText, dialogueOptions[dialogueIndex].Text, .05)

	if dialogueOptions[dialogueIndex].yesOption or dialogueOptions[dialogueIndex].noOption then
		yesButton.Text = dialogueOptions[dialogueIndex].yesOption
		noButton.Text = dialogueOptions[dialogueIndex].noOption
	else
		yesButton.Visible = false
		noButton.Visible = false
	end
	typing = false
end

local function startDialogue(npc, dialogueOptions)
	clearDialogue()

	activated = true
	nameLabel.Text = npc.Name
	dialogueUI.MainFrame.Position = UDim2.new(-2, 0,1, 0)
	yesButton.Position = UDim2.new(0.444, 0,-1, 0)
	noButton.Position = UDim2.new(0.444, 0,-1, 0)
	dialogueUI.Enabled = true
	tweenService:Create(dialogueUI.MainFrame, mainFrameTweenInfo, {Position = UDim2.new(0.009, 0,0.751, 0)}):Play()
	updateText(dialogueOptions)
	print(activatedModule.npcOptionsInformation())
	tweenService:Create(yesButton, buttonsTweenInfo, {Position = UDim2.new(0.445, 0,0.741, 0)}):Play()
	tweenService:Create(noButton, buttonsTweenInfo, {Position = UDim2.new(0.444, 0,0.857, 0)}):Play()
end

-- Main functions
function dialogueManager.dialogueMain(npc)
	if typing or activated then return end

	print("The NPC Clicked was: "..npc.Name)
	print("The available NPC dialogues are: ")
	print(dialogueInfoModules)
	for i, v in pairs(dialogueInfoModules) do
		activatedModule = require(v)
		if activatedModule.npcName() == npc.Name then
			print("Activating Module: "..activatedModule.npcName())
			print("The options for dialogue are: ")
			print(activatedModule.npcOptionsInformation())
			startDialogue(npc, activatedModule.npcOptionsInformation())
		end
	end
end

function dialogueManager.continueFunction()
	if typing then return end
	local npcOptionsInformation = activatedModule.npcOptionsInformation()
	print(activatedModule.npcOptionsInformation())
	dialogueIndex = npcOptionsInformation[dialogueIndex].Next
	sounds.ClickSound:Play()

	if not npcOptionsInformation[dialogueIndex].yesOption or not npcOptionsInformation[dialogueIndex].noOption then
		updateText(npcOptionsInformation)

		task.wait(1)

		tweenService:Create(dialogueUI.MainFrame, mainFrameTweenInfo, {Position = UDim2.new(-2, 0,1, 0)}):Play()
		tweenService:Create(yesButton, buttonsTweenInfo, {Position = UDim2.new(0.444, 0,-1, 0)}):Play()
		tweenService:Create(noButton, buttonsTweenInfo, {Position = UDim2.new(0.444, 0,-1, 0)}):Play()

		dialogueUI.Enabled = false

		dialogueUI.MainFrame.Position = UDim2.new(0.009, 0,0.751, 0)
		yesButton.Position = UDim2.new(0.445, 0,0.741, 0)
		noButton.Position = UDim2.new(0.444, 0,0.857, 0)

		clearDialogue()
	end
	updateText(npcOptionsInformation)
end


function dialogueManager.leaveFunction()
	if typing then return end

	local npcOptionsInformation = activatedModule.npcOptionsInformation()

	dialogueIndex = npcOptionsInformation[dialogueIndex].Negxt
	sounds.ClickSound:Play()
	
	updateText(npcOptionsInformation)

	task.wait(1)

	tweenService:Create(dialogueUI.MainFrame, mainFrameTweenInfo, {Position = UDim2.new(-2, 0,1, 0)}):Play()
	tweenService:Create(yesButton, buttonsTweenInfo, {Position = UDim2.new(0.444, 0,-1, 0)}):Play()
	tweenService:Create(noButton, buttonsTweenInfo, {Position = UDim2.new(0.444, 0,-1, 0)}):Play()

	dialogueUI.Enabled = false

	dialogueUI.MainFrame.Position = UDim2.new(0.009, 0,0.751, 0)
	yesButton.Position = UDim2.new(0.445, 0,0.741, 0)
	noButton.Position = UDim2.new(0.444, 0,0.857, 0)

	clearDialogue()
end

return dialogueManager

Anyone that can help, would be appreciated a lot.

1 Like

Fixed it myself. Added a variable for the NPCname set that variable to the correct npcname in the dialogue main. And then re-setting the activatedModule in the continue and leave function.

1 Like

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