Better way to do this

Alright so i have this script


local progressionConnection
local dialogueConnection
local event = game.ReplicatedStorage.GiveObjective

local player

local testDialogue = {
	{
		command = "Dialogue",
		DialogueText = "Hello"
	},



	{
		command = "Prompt",
		DialogueText = "Now you can choose",
		option1 = {
			text = "SayCat",
			followup = {
				{
					command = "Dialogue",
					DialogueText = "It works cat"
				},
				
				{
					command = "Prompt",
					DialogueText = "Another thing for the cat",
					
					option1 = {
						text = "First option",
						
						followup = {
							
							{
								command = "Prompt",
								DialogueText = "cat final question",
								
								option1 = {
									text = "Monkey",
								},
								
								option2 = {
									text = "Gorilla"
								}
							}
							
						}
						
					},
					option2 = {
						text = "Second option",
						
						followup = {
							{
								command = "Dialogue",
								DialogueText = "cat123"
							},
							{
								command = "Dialogue",
								DialogueText = "cat1234"
							},
							{
								command = "Dialogue",
								DialogueText = "cat12345"
							},
						}
					}
				},
			}
		},
		option2 = {
			text = "SayDog",
			followup = {
				{
					command = "Dialogue",
					DialogueText = "It works dog"
				}
			}
		},
	},
}

local dialogueIndex = 1

function Ui(plr, tableToUse, current)
	if not tableToUse[current] then return end
		print(tableToUse[1])
		local textLabel = plr.PlayerGui.Conversations.Frame.TextLabel
		local choice1 = textLabel.Parent.Choice1
		local choice2 = textLabel.Parent.Choice2

		-- Ensure the frame is visible
		plr.PlayerGui.Conversations.Frame.Visible = true

		if tableToUse[current].command == "Dialogue" then
			-- Set dialogue text and show the 'Next' button
			textLabel.Text = tableToUse[current].DialogueText
			choice1.Visible = true
			choice1.Text = "Next"
			choice2.Visible = false -- Hide the second choice

			-- Reset previous connections to avoid multiple triggers
			if dialogueConnection then dialogueConnection:Disconnect() end
			dialogueIndex += 1

			-- Connect the 'Next' button
			dialogueConnection = choice1.MouseButton1Click:Connect(function()
				Ui(plr, tableToUse, dialogueIndex)
			end)

		elseif tableToUse[current].command == "Prompt" then
			-- Set prompt text and show both choices
			textLabel.Text = tableToUse[current].DialogueText
			choice1.Visible = true
			choice2.Visible = true
			choice1.Text = tableToUse[current].option1.text
			choice2.Text = tableToUse[current].option2.text

			-- Reset previous connections to avoid multiple triggers
			if dialogueConnection then dialogueConnection:Disconnect() end
			--if progressionConnection then progressionConnection:Disconnect() end
			dialogueIndex += 1

			-- Connect both choice buttons
			dialogueConnection = choice1.MouseButton1Click:Connect(function()
				dialogueIndex = 1
				progressionConnection:Disconnect()
				Ui(plr, tableToUse[current].option1.followup, 1)	
			end)
			progressionConnection = choice2.MouseButton1Click:Connect(function()
				dialogueIndex = 1
			progressionConnection:Disconnect()
				Ui(plr, tableToUse[current].option2.followup, 1)
			end)
		end
end

game.Players.PlayerAdded:Connect(function(plr)
	task.wait(2)
	player = plr
	Ui(plr, testDialogue, 1)
end)


--[[function entertedRestaurant()
	progressionConnection = game.Workspace.MainDoor.PromptPart.ProximityPrompt.Triggered:Connect(function()
		progressionConnection:Disconnect()
		event:FireClient(player, "empty", "hide")
		task.wait(2)
		event:FireClient(player, "Go to the staff room and clock in", "show")
		
	end)
end
--]]

seeing it you probably immediately thought: “What is that massive table” and thats the issue i want to address. the script works as intended. However that table is just so ugly to look at and i know it will by a nightmare to modify to it, anyways is there a better way to do this, somehow avoiding that massive table? For now its just for testing and hasn’t further been implemented into the game so now is the best time to change it if need be

The code comments where after I got an ai to remove any potential bugs but the table monstrosity was my idea. Ai didn’t do much it just added checks everywhere that literally can’t fails regardless

Main ways I can think of is to have the table stored in a Module Script (and then access that table within your main script) or make use of actual objects such as Folders and StringValues to recreate the table.

The best option is probably the module script.

Use a stack to manage the dialogue state.

That one script is the only thing that would reference the table, it’s a single player game and there no need for another script to ever reference the table

What does that mean. What is a stack

A stack is a type of array (table) that can remove items from the top and add items from the bottom

adding to a stack

item 1
item 2

-- add a item:

item 1
item 2
item 3

now removing

item 1
item 2
item 3

-- remove a item

item 2
item 3
2 Likes

Still, it can be useful to contain large arrays in seperate scripts so that the main script is more legible and you don’t have to scroll for 10 years to get to the actual code.

2 Likes

A stack is used to keep track of previous dialogues.

1 Like

Could you show how that would work or how I would do that?

With how my code functions, having multiple possible outcomes given on what the player does would a stack be better or possible then my current way?

Yeah i might try using the stacks others are saying and if I can’t figure that out I’ll just move it to a module because yea id like to not have to scroll through that whole array

I think stacking is the best option right now.

Stacking seems to be more complex. I never actually thought about just moving the table to a module and I also realized I can get a ai to format the script given my instructions so I’ll likely do that. Thanks for the advice still, I’ll consider using stacks in the future

local testDialogue = {
    Dialogue.new("Hello", {
        { text = "SayCat", followup = {
            Dialogue.new("It works cat", {}),
            Dialogue.new("Another thing for the cat", {
                { text = "First option", followup = {
                    Dialogue.new("cat final question", {
                        { text = "Monkey", followup = {} },
                        { text = "Gorilla", followup = {} }
                    })
                }},
                { text = "Second option", followup = {
                    Dialogue.new("cat123", {}),
                    Dialogue.new("cat1234", {}),
                    Dialogue.new("cat12345", {}),
                }},
            }},
        }},
        { text = "SayDog", followup = {
            Dialogue.new("It works dog", {}),
        }},
    }),
}

local dialogueStack = {}
local currentDialogue

local function Ui(plr)
    if not currentDialogue then return end
    
    local textLabel = plr.PlayerGui.Conversations.Frame.TextLabel
    local choice1 = textLabel.Parent.Choice1
    local choice2 = textLabel.Parent.Choice2
    local choices = currentDialogue.options
1 Like

Not too hard, just takes a little time.

It’s harder for me too see progression in the stack format but I’m sure it can be useful in the future for me. Thanks

Perhaps you could do that, and code in a custom dialogue editor for yourself? (Its extra work ig)

1 Like

You mean like a plugin? Never made those I assume it’s difficult

Not neccessarily a plugin, since its only for your game, you could just make some sort of admin UI or something that you can run (in the “run” mode) to more easily see how it works.

1 Like