Help Making an NPC Dialogue Handler

Hello all,
I am working on some NPCs for a game of mine. I already made working dialogue for the NPCs, which consists of a GUI with the NPC’s Name, image, and a typewriter-style text. (See below)

I would like to make a Handler that can essentially get each NPC’s information (Name, Dialogue, amount of dialogue lines, etc) and send it to the local player with one script so that I don’t need to make a new script for each NPC’s dialogue, however, each NPC will have a different name, different dialogue, and some NPCs will have more dialogue lines than others.
How would I go about making this without it being a messy copy and paste?

1 Like

You could use OOP!
(Object Oriented Programming)

thank you for your detailed explanation on how to do this

I don’t know if you know what OOP is but if you don’t, learn it because you could do it like this

Dialogue.new()
   
end

Dialogue:NewText(Npc)

end

Dialogue:End()

end
3 Likes

You could use a modulescript to send dialogue, and have custom triggers for each NPC in their script. This way you can easily modify the dialogue system in the modulescript, and you just use the npc script to call the modulescript

--within npc, way easier to do in the grand scheme of things
local npc = script.Parent
local npcDialogueSystem = require(NpcDialogueSys)

local function ActivateDialogue()
    npcDialogueSystem.OnActivated(npc)
end

If you want all npcs to use one script for their dialogue, you can do something like a i, v or listen to a childadded for all the npcs you need. Somewhere in ServerScriptService, something like

--within serverscriptservice, harder to do, and with module scripts i personally wouldn't
local dialogue = require(script.Dialogue)

for _, NPC in NPCTable do
    dialogue.ListenForTriggered(NPC)
end

In your case, listen for trigger would probably be a .clickdetector or something based on the NPC.
Store the npc’s dialogue in a modulescript dictionary inside the NPC, so like

-- a modulescript inside the npc to access the dialogues from
return {
["NpcName"] = "The Smith";
["Dialogues"] = {
    [1] = "If you need to upgrade or buy gear, I'm here"; 
    [2] = "Hm.. Give me three ancient stones if you want that upgrade";
    [3] = "Long ago, a creature from the cosmos above fell on this land. It made sure we had no choice but to run or be cursed...";
   }
}
1 Like

Second this. OOP is very useful if you’re making a game with NPCS involved. Haven’t really gone into it myself tho

You could have one module script containing tables with all the information of the NPC’s. Then once the player clicks on the npc fire a remote function to retrieve the table of the NPC for use. I’ll leave it to you to decide how to structure the dialogue code.

1 Like

There’s no point to reach for objects here - trying to contort everything into an object is one of Roblox’s worst antipatterns. There will only be one chatted text on the screen, an object only makes sense if you’re going to have multiple versions of something existing at the same time.

You can do this with a simple function on the client. Including a skip dialogue speed state to show how state can exist independent of objects just fine.

local letterYield = .4

local function displayText(textToDisplay, avatarThatSaidIt)
    gui.TextLabel.MaxVisibleGraphemes = 0
    gui.TextLabel.Text = textToDisplay

    gui.AvatarImage.Image = avatarThatSaidIt

    letterYield = .4 -- Reset to handle previous skip

    local textLength = string.len(textToDisplay) -- Gets character count for animation

    for i = 1, textLength do
        gui.TextLabel.MaxVisibleGraphemes += 1
        task.wait(letterYield)
    end
end

NPCChattedRemote:Connect(displayText)

gui.SkipButton.Activated:Connect(function()
    letterYield = .2 -- Go 2x faster
end)
2 Likes

You’re right but I think dialogue IS something to be used more than once. For example, having more than one npc. You would add a Dialogue module in ReplicatedStorage and than add a module in any of the NPCs that controls what the npc will say. It’s also better to do this so if you need to update the dialogue module, it replicates it for all NPCs since they all use that one module

ModuleScripts worked best for me, I don’t know why I hadn’t thought of that lol
Thank you for your help!

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