Optimizations for function?

Hey, I’m working on a story game and one of the functions within this script that manages a part of the game has become a bit messy and long. Is there an optimizations I could make for this function to be shorter and more organized?

Function:

local function Part3()
	
	local MeteorObjective = Objectives:FindFirstChild("MeteorObjective"):Clone()
	
	local TweenService = game:GetService("TweenService")
	local Info = TweenInfo.new(3, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)

	local FallTween = TweenService:Create(MeteorObjective:FindFirstChild("Meteor"), Info, {CFrame = MeteorObjective:FindFirstChild("FallenPosition").CFrame})
	
	TransitionEvent:FireAllClients()
	wait(2)
	
	game.Lighting.TimeOfDay = 1
	game.Lighting.Brightness = 1
	game.Lighting.Ambient = Color3.new(0, 0, 0)
	game.Lighting.OutdoorAmbient = Color3.new(0, 0, 0)
	
	StopSoundEvent:FireAllClients()
	PlaySoundEvent:FireAllClients(RelaxedScene, 0)
	
	TeleportPlayers(Teleports:FindFirstChild("CabinTeleport").CFrame)
	wait(5)
	
	ToggleDialogEvent:FireAllClients()
	
	DialogEvent:FireAllClients(NpcImage, NpcName, "It's getting dark. We should go inside the cabin now.", RedText)
	wait(5)
	RandomPlayer() DialogEvent:FireAllClients(PlayerImage, PlayerName, "Yeah, I'm getting sleepy.", BlueText)
	wait(5)
	RandomPlayer() DialogEvent:FireAllClients(PlayerImage, PlayerName, "Wait what's that noise?", BlueText)
	wait(5)
	
	StopSoundEvent:FireAllClients()
	PlaySoundEvent:FireAllClients(Scary, 0)
	
	Monster.Parent = game.Workspace:FindFirstChild("Npcs")
	wait(1)
	
	RandomPlayer() DialogEvent:FireAllClients(PlayerImage, PlayerName, "Why is there a monster!?", BlueText)
	wait(5)
	DialogEvent:FireAllClients(MonsterImage, MonsterName, "We've waited long enough.", DarkBlueText)
	wait(5)
	DialogEvent:FireAllClients(MonsterImage, MonsterName, "We'll give you one chance to leave.", DarkBlueText)
	wait(5)
	DialogEvent:FireAllClients(NpcImage, NpcName, "No! This is our cabin and you aren't taking it!", RedText)
	wait(5)
	DialogEvent:FireAllClients(MonsterImage, MonsterName, "You fools! You'll have to leave the hard way.", DarkBlueText)
	wait(5)
	
	ToggleDialogEvent:FireAllClients()
	wait(1)
	
	MeteorObjective.Parent = game.Workspace
	
	FallTween:Play()
	wait(3)
	
	ToggleDialogEvent:FireAllClients()
	
	RandomPlayer() DialogEvent:FireAllClients(PlayerImage, PlayerName, "The monster destroyed the cabin!", BlueText)
	wait(5)
	DialogEvent:FireAllClients(NpcImage, NpcName, "We need to hide in the cave!", RedText)
	wait(5)
	
	ToggleDialogEvent:FireAllClients()
	ToggleObjectiveEvent:FireAllClients()
	
	ObjectiveEvent:FireAllClients("Hide in the cave.")
	
	Triggers:FindFirstChild("CaveTrigger").Parent = game.Workspace
	
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v.Name == "CaveTrigger" then
			v.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					v:Destroy()
				end
			end)
		end
	end
	
	repeat
		wait()
	until game.Workspace:FindFirstChild("CaveTrigger") == nil
	
	ToggleObjectiveEvent:FireAllClients()
	
	TransitionEvent:FireAllClients()
	wait(2)
	
	TeleportPlayers(Teleports:FindFirstChild("CaveTeleport").CFrame)
	Npc:SetPrimaryPartCFrame(Teleports.CaveTeleport.CFrame)
end
1 Like

I’d recommend you instead use a module script. It’ll make it a lot cleaner.

1 Like

Thanks for the suggestion, I’m not that experienced with module scripts, but I will consider doing some research on it.

It’s actually simple. Heres how to do it:
Module Script:

-- module script in rep storage
local module = {}
function module.new(PlrName, PlrImage, Message, Color) -- this is the function we'll use to make the things happen
	DialogEvent:FireAllClients(PlrImage, PlrName, Message, BlueText)
end
return module

Server script:

-- script in server script service
local StoryModule = require(game.ReplicatedStorage.StoryModule)
StoryModule.new(PlayerName, PlayerImage, "IM SO COOL", Color3.fromRGB(0, 46, 255))

that simple

1 Like

You can put all the text in a table and just make a function play the next line.
Example:


local CurrentLine = 0
local Lines = {
	{"Oh no a monster!", Color3.fromRGB(255, 255, 255), "Bob"},
	{">:D", Color3.fromRGB(255, 0, 0), "Monster"},
	{"AHHH!", Color3.fromRGB(255, 255, 255), "Bob"}
}

function PlayNextLine()
	CurrentLine += 1
	Event:FireAllClients(Lines[CurrentLine])
end

PlayNextLine()
task.wait(2)
PlayNextLine()
task.wait(4)
PlayNextLine()
1 Like

My reply only matters if you’re calling this function a ton of times within a short amount of actual time.

Is it possible you can reuse the MeteorObjective instance instead of cloning it everytime the function is called?

For example, whenever a MeteorObjective is no longer in use, instead of destroying it, you parent it a folder in ServerStorage and instead of using Clone() every time the function is called, you’ll just grab one from the storage folder.

Since I don’t know how this function is used, or if MeteorObjective isn’t an object with a lot of properties/children, this might not be necessary.

1 Like