What is the most efficient and easy way you developers have scripted a dialogue system?

Should I just hardcode it like

if (i chose response #1 to the NPCs first dialogue) then
    present second dialogue with response_option_A, response_option_B, response_option_C
    if (i choose response_option_A) then
        present third dialogue with Yes or No
        if (person clicked yes)
            do action 1
        else
            do action 2
    if (i choose response_option_B) then
        present fourth dialogue with "I see, so that's how it is" and "I don't get it, can you repeat that?"
        if (i choose "I see, so that's how it is") then
            move on
        else
            repeat text
elseif (i chose response #2 to the NPCs dialogue) then
    present present third dialogue
elseif
... etc.

so one response leads to another, or is there a more efficient way of reusing code so I could make new NPCs with new dialogues super easily? i tried making this wack system using modulescripts with nested dictionaries, pairing each dialogue with a list of responses, but it didn’t work. How would you guys go about it?

Please feel free to share your thoughts.

Edit: Sorry, I think I somehow must have explained what I was asking for incorrectly, I edited the pseudocode above so it’s more specific now.

I’m looking for a dialogue system that the player can respond to, but there will be multiple different responses you can choose. for example, if an NPC speaking to me said:
“Do you have a sword?”
then there would be 3 responses

  1. Yes
  2. No
  3. I decline to say.
    and whatever response you choose, another dialogue shows up. For example, if you choose “Yes” maybe the NPC will display “Oh, that’s great, I need you to defeat 3 goblins” and would give you 2 responses to that such as “Okay, I accept the mission” or “No, this sucks, count me out.” If you instead chose “No”, the NPC would say “Well, I need you to go get a sword” then he sends you on a mission to get a sword, and finally, if you choose “I decline to say”, the NPC would probably say “darn, you’re one boring kid, you know that?”

See how the dialogue branches out? one response leads to 3 more, and each of those 3 responses leads to 2 more responses, and maybe those responses lead to more. You get the point, right?

2 Likes

I love dialogue systems! And I have to say, hardcoding takes the fun out of it. A good dialogue system should just be able to take in some data related to what to say, the speaker’s name, etc. and then just turn that into a dialogue box that can be clicked through.

What I prefer to do is have a folder in ServerStorage named Dialogue or something like that.

For each set of dialogue, create a folder parented to it. You could name it whatever you like, maybe “BobDialogue1.” It just needs to be referred to later.

Then I put in StringValues with names from 1, 2, 3, 4, all the way up to however many lines I want. In the StringValue’s value, I can put the name of the person speaking.

Then you could add another StringValue parented to the original one named “Message” and set the value to whatever they’re supposed to say.

One of my first Dialogue systems was a bit more convoluted and had things like the speaker’s icon image, the color of their background to represent if they were a good guy or a bad guy, etc. I had it structured like so.
image

Then, ideally, you’d want the Server to have a function called SendDialogue() and pass in a parameter like “BobDialogue1” from earlier to tell the function to search the Dialogue folder for this specific set of lines into a table so that it can be looped through in a for loop, get the name of the speaker, etc and then send that info to the Client via a RemoteEvent.

Then have a LocalScript on the client listening to that RemoteEvent and create a frame with a TextLabel of the speaker’s name and a TextButton of the first line. Use TextButton.MouseButton1Up:Connect(function() to wait for the player to click the TextButton and it’ll move on to the second line in the table of lines given. Once all the lines are clicked through, destroy the frame. That would be a very basic type of dialogue system.

Dialogue systems can be really convoluted or really simple. It’s hard to tell you exactly how to do yours. My best advice is to take it one step at a time. Don’t try to think of everything at once.

I often like to watch a video of someone making their own versions so that I can at least have a basic version and then try improving it myself. Here’s one I just found if you want.

1 Like

While @Physicism’s method works I don’t like working with instances and prefer data in the form of code. Each of those instances is a lot more expensive than raw data. My preferred method is using a ModuleScript that returns the data itself. There’s also no reason to keep this data in the server as its just dialogue and there’s no harm done if an exploiter alters it for their own client so you can put it in ReplicatedStorage. Here’s an example:

return {
  Bob = {
    Image = 'rbxassetid://...',
    Status = '...',
    Title = 'Some Title',
    Messages = {
      'Hello, my name is bob!',
      'This is the second message.',
    },
  },
}

Then you can easily access this data:

local DialogueData = require(game:GetService('ReplicatedStorage').DialogueData)

print(DialogueData.Bob.Messages[2]) -- Print Bob's second message

-- You can also write utility methods in another module script to make navigating the data easier
local function getMessage(name: string, index: number)
  return DialogueData[name].Messages[index]
end

print(getMessage('Bob', 1)) -- Print Bob's first message

1 Like

Sorry, I think I somehow must have explained what I was asking for incorrectly, I edited the pseudocode so it’s more specific now.

I’m looking for a dialogue system that the player can respond to, but there will be multiple different responses you can choose. for example, if an NPC speaking to me said:
“Do you have a sword?”
then there would be 3 responses

  1. Yes
  2. No
  3. I decline to say.
    and whatever response you choose, another dialogue shows up. For example, if you choose “Yes” maybe the NPC will display “Oh, that’s great, I need you to defeat 3 goblines” and gives you a mission to defeat 3 goblines, while if you instead chose “No”, the NPC would say “Well, I need you to go get a sword” then he sends you on a mission to get a sword, and finally, if you choose “I decline to say”, the NPC would probably say “darn, you’re one boring kid, you know that?”

See how the dialogue branches out? one response leads to 3 more, and each of those 3 responses leads to 2 more responses, and maybe those responses lead to more. You get the point, right?

The concepts of nodes, graph, and the data structure explained here:

Someone already made it, with visual style programming. Also uses the same concepts of nodes, graph but pluginized with a very neat looking GUI like something out of unreal engine.

2 Likes

this was exactly what i was looking for, thank you!