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