Making Randomized Dialogue... again

Basically I have an NPC that can speak to you. It’s pretty boring if I just let them speak the same thing over and over again, so I wanted to have randomized dialog strings each time you interact with the NPC. The way I thought of is:

prompt.Triggered:Connect(function()
	Random_Dialog = math.random(1,3)
	if Random_Dialog == 1 then
		Random_Dialog = 0 --resets the random dialog number
	elseif Random_Dialog == 2 then
		Random_Dialog = 0
	elseif Random_Dialog == 3 then
		Random_Dialog = 0
	end
end)

But I feel like it’s inefficient or doesn’t look “clean”. How can I make this code more proper?

10 Likes

You can have a table with all the strings, and then make the prompt select a random string from the table.

local StringTable = {
	"The first string in the table!",
	"Second string, second place!",
	"Third string, way to go!"
}

prompt.Triggered:Connect(function()
	Random_Dialog = StringTable[math.random(1,#StringTable)]
end)
4 Likes

Ahh that’s not what I meant. I have dialogues already set up, I just want each path to be “randomized” so it’s not the same dialog the second time

One path would be

Hello
How are you doing
Goodbye

And another would be

Sup
What’s going on
Cya

Or

Whats up
The weathers nice
Bye

Then I could change each interaction to be different by getting another path thats not been said

1 Like

mm I don’t know much about Roblox’s dialog system, but I guess you could parent a new dialog to the NPC once the current one is done? Like, when the current dialog has ended, it chooses a new dialog that is parented to the script, parents the current dialog back to the script, and then parents the new one to the NPC, and then it continues.

I don’t really know how to explain :skull:

3 Likes

It’s a custom dialog gui, not the Dialog system in Roblox :sweat_smile:

2 Likes

Ah, I recently made something similar then! When you begin the dialog, you simply choose a new random one from a table or anywhere you want (it’s up to you), remove it from the table, add the PREVIOUS dialog back to the table, and from there you simply display it.

local PreviousDialog = nil

prompt.Triggered:Connect(function()
	local Index = math.random(1,#Dialogs)
	local NewDialog = Dialogs[Index]
	table.remove(Dialogs,Index)
	
	if PreviousDialog then
		table.insert(Dialogs,PreviousDialog)
	end
	PreviousDialog = NewDialog
	
	-- display the new dialog, do whatever you want
end)
2 Likes

have tables in tables (like metatables)

local defaultAmount = 3 --default amount of dialogs
local firstMessages = {
   "hey!";
   "whaddaya need?";
}

local secondMessages = {
   "the weather's nice";
   "how are you?";
}

local thirdMessages = {
   "cya!";
   "bye bye";
}

local dialogs = {}

function setDialogs(amount) --amount is the amount of dialogs
   math.randomseed(math.random(1, 100000))
   table.clear(dialogs)
   if amount <= 0 then return end
   for i = 1, amount do
      local dialog = {firstMessages[math.random(1, #firstMessages)], secondMessages[math.random(1, #secondMessages)], thirdMessages[math.random(1, #thirdMessages)]}
      table.insert(dialogs, dialog)
   end
end

function pickDialog()
   math.randomseed(math.random(1, 100000))
   if not (#dialogs > 0) then setDialogs(defaultAmount) end
   return dialogs[math.random(1, #dialogs)]
end

prompt.Triggered:Connect(function()
   local dialog = pickDialog()
end)
3 Likes

I’ll clarify this part a bit more

I have multiple sets of dialogs that an NPC can take to interact with the player, they are already clarified and set, there’s no tables to this.

Path/Dialogs 1

typeWrite("Hey!")
..."How's it going?" --"..." is just short for typeWrite, hassle to type
..."Stay safe my friend."

Path/Dialogs 2

..."Sup!"
..."You chilling?"
..."Cya later mate."

Basically, I want it to select one set of dialogs and do those only:

--This is only a rough sketch of the base dialogs
prompt.Triggered:Connect(function()
	Random_Dialog = math.random(1,3)

	if Random_Dialog == 1 then
		typeWrite("Sup man, how's it going?")
		task.wait(0.5)
		typeWrite("This weekend's been a doozy, ain't it?")
		task.wait(0.5)
		typeWrite("Alright, catch you later man.")
		task.wait(0.5)
		Random_Dialog = 0 --resets the random dialog number
	elseif Random_Dialog == 2 then
		typeWrite("blah blah")
		task.wait(0.5)
		typeWrite("blah blah")
		task.wait(0.5)
		typeWrite("blah blah")
		task.wait(0.5)
		Random_Dialog = 0 
		--and so on and so forth

How would I go about this in the most efficient way?

local dialogues = {
	{"text", "text2", "text3"}, -- dialogue 1
	{"text4", "text5", "text6"} -- dialogue 2
}

local dialogue = dialogues[math.random(#dialogues)]
for _, text in dialogue do
	
end

Base it off of this, you could also edit it to add customization to each text entry (if you need a different delay time, let’s say)

2 Likes
local paths = {
	{"Hello", "How are you doing", "Goodbye"},
	{"Sup", "What’s going on", "Cya"},
	{"Whats up", "The weathers nice", "Bye"}
}

local randomPath = paths[math.random(#paths)]

-- Loop thru dialog in path
for i, dialog in randomPath do
	-- Display it?
end
1 Like
local dialogs = {
   Dialog1 = {"Hey!", "The weather's nice, ain't it?", "Bye!"};
   Dialog2 = {"yo!", "what's good?", "cya bro"};
   Dialog3 = {"wassup", "how's life?", "i hate mine", "wait this is the fourth text?", "and the fifth?! well i gotta go now cya"};
   --and so on
}

prompt.Triggered:Connect(function()
   task.spawn(function()
      math.randomseed(1, 100000)
      local dialog = dialogs["Dialog"..tostring(math.random(#dialogs))]
      for _, text in dialog do
         typeWrite(text)
         task.wait(0.5)
      end
   end)
end)
1 Like

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