Is having an unorganize scripts like this bad?

Hello! I am wondering if having these unoganize scripts like this is bad or annoying because we would have to duplicate the line everytime to play a dialogue and change the timing. I was thinking ot make them a function but I can’t seem to figure it out.

Here is the script:

local Trigger = workspace:WaitForChild("Dialogue"):WaitForChild("D1")
local Sound1 = workspace:WaitForChild("DialogueSound"):WaitForChild("Sound")
local Objective = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("Objectives"):WaitForChild("ObjectiveLabel2")
local function typing(Objective, text)
	for i = 1,#text do
		Objective.Text  = string.sub(text,1,i)
		wait(0.055)
	end	
end
local function Sequence_1(p4, p5, p6, p7, p8)
	if not p7 then
		typing(p4, p5, p8)
	else
		p4.Text = p5
	end
	wait(p6 or 2)
end

Sequence = Sequence_1
Sequence_1 = Trigger.Touched
Sequence_1:Connect(function(p9)
	Trigger:Destroy()
	Objective.TextTransparency = 0
	game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
	typing(Objective, "Huh, why am I here..?", 3, true, 0.05)
	wait(1.7)
	Objective.Text = ""
	Objective.TextTransparency = 1
	wait(0.1)
	Objective.TextTransparency = 0
	game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
	typing(Objective, "I remembered that I was at the cave trying to find the flashlight I left..", 3, true, 0.05)
	wait(2)
	Objective.Text = ""
	Objective.TextTransparency = 1
end)

Thank you so much for your help!

Hi!
You should consider making a module script.

You add a ModuleScript into ServerStorage/ServerScriptService then require it from all the other scripts you are trying to replace.

In the scripts you are replacing:

local MyDialogModule = require(game.ServerStorage.TheModuleYouMade)
MyDialogModule.DoThisThingWith(script.Parent.Frame.TextLabel, "This Arugment you can pass!")

In the module:

local module = {}

function module.DoThisThingWith(ArgumentA, ArgumentB)
   ArgumentA.Text = ArugmentB
end

return module

Here’s a useful video.

1 Like

I’m a little confused on this how would I connect my orginal script to this new one? Also, the script above is a local script btw!

1 Like

You can connect module scripts to original scripts by using “require”, which will get a piece of information from the module script so that you do not have to write it over and over again. The Roblox devhub and API reference explains it better than me so here it is:

“ModuleScripts are essential objects for adhering to the don’t-repeat-yourself (DRY) principle. When you write a function, write it only once and use it everywhere. Having multiple copies of a function is disastrous when you need to change that behavior. So, you should define functions or groups of functions in ModuleScripts and have your Scripts and LocalScripts call require on your ModuleScripts. Keep your code organized!”

https://developer.roblox.com/en-us/api-reference/class/ModuleScript