Custom Dialogue System

  1. What do you want to achieve? Keep it simple and clear!
    I’ve been making a custom dialogue system where the NPC says “hello” and the player is given 3 different options. Each option makes the NPC say something different and then they are given three more options which give the NPC another different reply and this will end eventually.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is that it only works with option one, and even then it only works halfway. I keep getting hit with the same error when clicking on option 2 and option 3:
    Players.yellowmonster110111.PlayerGui.DialogueHandler:24: attempt to index nil with ‘dialogue’ - Client - DialogueHandler:24
    I usually figure out these things on my own but it’s been 2 days and I cannot figure out what’s wrong.

Here’s a video of it working for option 1:

External Media

Here’s a video of what happens when you click option 2 or 3:

External Media
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    So I’ve tried changing the script up multiple times as you can see with the starting off variable which is now useless I just forgot to remove it.

This is the server script which contains the full dialogue list and when you interact with its parent proximity prompt, it fires your client.

local dialoguelist = {
	NPCdialogue = {
		{
			dialogue = "NPC: hi",
			replies = {
				{
					dialogue = "PLAYER: option 1",
					replies = {
						{
							dialogue = "NPC: That's a great choice!",
							replies = {
								{
									dialogue = "PLAYER: option 1.1",
									replies = {
										{
											dialogue = "NPC: Well done!",
										},
									},
								},
								{
									dialogue = "PLAYER: option 1.2",
									replies = {
										{
											dialogue = "NPC: Nice work!",
										},
									},
								},
								{
									dialogue = "PLAYER: option 1.3",
									replies = {
										{
											dialogue = "NPC: Great decision!",
										},
									},
								},
							},
						},
					},
				},
				{
					dialogue = "PLAYER: option 2",
					replies = {
						{
							dialogue = "NPC: That's an interesting choice!",
							replies = {
								{
									dialogue = "PLAYER: option 2.1",
									replies = {
										{
											dialogue = "NPC: Good thinking!",
										},
									},
								},
								{
									dialogue = "PLAYER: option 2.2",
									replies = {
										{
											dialogue = "NPC: Excellent idea!",
										},
									},
								},
								{
									dialogue = "PLAYER: option 2.3",
									replies = {
										{
											dialogue = "NPC: Nice job!",
										},
									},
								},
							},
						},
					},
				},
				{
					dialogue = "PLAYER: option 3",
					replies = {
						{
							dialogue = "NPC: That's a bold choice!",
							replies = {
								{
									dialogue = "PLAYER: option 3.1",
									replies = {
										{
											dialogue = "NPC: Good for you!",
										},
									},
								},
								{
									dialogue = "PLAYER: option 3.2",
									replies = {
										{
											dialogue = "NPC: Excellent decision!",
										},
									},
								},
								{
									dialogue = "PLAYER: option 3.3",
									replies = {
										{
											dialogue = "NPC: Well done!",
										},
									},
								},
							},
						},
					},
				},
			},
		},
	},
}
script.Parent.Triggered:Connect(function(player)
	game.ReplicatedStorage.DialogueRemote:FireClient(player,script.Parent.Parent,"hi",dialoguelist)
end)

Here is the Local Script, it creates a new gui on event fire and has a function which uses the dialogue list to check replies:
local function UpdateDialogue(gui, currentdialogue, dialogueList, startingoff)

if currentdialogue then
    print(currentdialogue)
    print("Current dialogue:", currentdialogue.dialogue)
    print("Replies:", currentdialogue.replies)

    for i, v in pairs(currentdialogue.replies) do

        print("Option:", i, v.dialogue)

        local clone = gui.BackGround.OptionTemplate:Clone()
        clone.Parent = gui.BackGround.Options
        clone.Text = v.dialogue

        clone.Visible = true

        clone.MouseButton1Click:Connect(function()
            if currentdialogue.replies then
                currentdialogue = currentdialogue.replies[i]
                print("New current dialogue:", currentdialogue.dialogue)
                print("New replies:", currentdialogue.replies)

                gui.BackGround.Dialogue.Text = currentdialogue.replies[i].dialogue

                for i, v in pairs(gui.BackGround.Options:GetChildren()) do
                    if v:IsA("TextButton") then
                        v:Destroy()
                    end
                end

                UpdateDialogue(gui, currentdialogue.replies[i], dialogueList)
            else
                gui:Destroy()
            end
        end)
    end
else
    gui:Destroy()
end

end
game.ReplicatedStorage.DialogueRemote.OnClientEvent:Connect(function(target, dialogue, dialogueList, eventDialogue, event)
local currentdialogue = dialogueList.NPCdialogue[1]
if not game.Players.LocalPlayer.PlayerGui:FindFirstChild(“DialogueGui”) then
local gui = game.ReplicatedStorage.DialogueGui:Clone()
gui.Parent = game.Players.LocalPlayer.PlayerGui
gui.Adornee = target.HumanoidRootPart

	gui.BackGround.Dialogue.Text = dialogue

	gui.BackGround.Dialogue.Text = currentdialogue.dialogue
	print("working")
	UpdateDialogue(gui, currentdialogue, dialogueList, true)
end

end)
Here are the errors I recieve if I select options 2 or 3 as well as 1.1 , 1.2 and 1.3:
Players.yellowmonster110111.PlayerGui.DialogueHandler:17: attempt to index nil with ‘replies’ - Client - DialogueHandler:17
Players.yellowmonster110111.PlayerGui.DialogueHandler:21: attempt to index nil with ‘dialogue’ - Client - DialogueHandler:21

I will be eternally grateful to whoever can help me fix this mistake.

1 Like

Turns out I was just being dumb lol. The list wasn’t formatted correctly, making the NPC dialogues their very own lists. Here’s the fixed code if anyone is curious:

local dialoguelist = {
	NPCdialogue = {
		{
			dialogue = "NPC: hi",
			replies = {
				{
					dialogue = "PLAYER: option 1",
					replies = {
						
							dialogue = "NPC: That's a great choice!",
							replies = {
								{
									dialogue = "PLAYER: option 1.1",
									replies = {
										
											dialogue = "NPC: Well done!",
										
									},
								},
								{
									dialogue = "PLAYER: option 1.2",
									replies = {
										
											dialogue = "NPC: Nice work!",
										
									},
								},
								{
									dialogue = "PLAYER: option 1.3",
									replies = {
										
											dialogue = "NPC: Great decision!",
										
									
								},
							},
						},
					},
				},
				{
					dialogue = "PLAYER: option 2",
					replies = {
						
							dialogue = "NPC: That's an interesting choice!",
							replies = {
								{
									dialogue = "PLAYER: option 2.1",
									replies = {
										
											dialogue = "NPC: Good thinking!",
										
									},
								},
								{
									dialogue = "PLAYER: option 2.2",
									replies = {
										
											dialogue = "NPC: Excellent idea!",
										
									},
								},
								{
									dialogue = "PLAYER: option 2.3",
									replies = {
										
											dialogue = "NPC: Nice job!",
										
									
								},
							},
						},
					},
				},
				{
					dialogue = "PLAYER: option 3",
					replies = {
						
							dialogue = "NPC: That's a bold choice!",
							replies = {
								{
									dialogue = "PLAYER: option 3.1",
									replies = {
										
											dialogue = "NPC: Good for you!",
										
									},
								},
								{
									dialogue = "PLAYER: option 3.2",
									replies = {
										
											dialogue = "NPC: Excellent decision!",
										
									},
								},
								{
									dialogue = "PLAYER: option 3.3",
									replies = {
										
											dialogue = "NPC: Well done!",
										
									
								},
							},
						},
					},
				},
			},
		},
	},
}
script.Parent.Triggered:Connect(function(player)
	game.ReplicatedStorage.DialogueRemote:FireClient(player,script.Parent.Parent,"hi",dialoguelist)
end)

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