Working with NPC Interaction

I’m working on an interaction module and feel as if I’ve created something rather long-winded. I feel as if the tree I’ve created is far too linear?

If anyone has suggestions, or even a better way of doing this I am open to ALL suggestions! Thank you.

local System = {}
local Debounce = false

function System.Configure(Player, UI, Obj)
if Debounce == false then else return end
Debounce = true
	
	System = {Player = Player, UI = UI, Obj = Obj}
	
	local Humanoid = Player.Character:WaitForChild("Humanoid")
	Humanoid.WalkSpeed, Humanoid.JumpPower = 0, 0
	
	UI.Dialog.Visible = true
	UI.Confirm.Label.Visible = false
	
Default()
end

local Dialog

function Text()
	for i = 1, #Dialog  do
	    System.UI.Dialog.DialogText.Text = string.sub(Dialog, 1, i)
	    wait(0.05)
	end   
end

function Default()
	System.UI.Dialog.DialogName.Text = System.Obj.Parent.Name
	
	Dialog = "Welcome, " .. System.Player.Name .. "!"
	Text()
	
	wait(1)
	Options_Set1()
end

function Options_Set1()
	local DialogOptions =
		{"Where am I?", "Who are you?", "Good-Bye!"}
	
	Dialog = "How can I be of assistance today?"
	Text()
	
	wait(1)
	
	for _,Existing in pairs (System.UI.Options:GetChildren()) do
	if Existing:IsA("TextButton") then
		if Existing.Name == "Template" then
		else Existing:Destroy() end
	end end
	
	for _,O in pairs (DialogOptions) do
		local TextButton = System.UI.Options.Template:Clone()
		TextButton.Parent = System.UI.Options
		TextButton.Visible = true
		TextButton.Text = O
		TextButton.Name = "Option"
		TextButton.MouseButton1Down:Connect(function()
		if TextButton.Text == DialogOptions[1] then Option_1()
		elseif TextButton.Text == DialogOptions[2] then
		elseif TextButton.Text == DialogOptions[3] then
		end
		end)
	end
end

function Option_1()
	for _,Existing in pairs (System.UI.Options:GetChildren()) do
	if Existing:IsA("TextButton") then
		if Existing.Name == "Template" then
		else Existing:Destroy() end
	end end
	
	Dialog = "You're currently in 'The Hub' - This is a core location for members of AtroLyt."
	Text()
	
	wait(1)
	
	Options_Set1()
end

return System
1 Like

You’re looking for the #development-support:code-review category.

I skimmed over your code and I notice a lack of abstraction in a lot of areas where it would be helpful. There’s lots of hard-coding and your system seems to lack support for an arbitrary number of dialogue options.

On that note, are you making an NPC dialogue system? You can probably borrow some tidbits from the Roblox Dialogue Editor, since it’s designed to hold dialogue trees and mapping. It is highly customisable and contains a lot of what you could want in for dialogue management.

The project is not actively maintained however and it seems like it’s been dropped, so you’ll have to look to supported alternatives being maintained by the community. The original code is still salvageable nonetheless.

2 Likes