A lot of other people have covered breaking your script into chunks, but I thought it wouldn’t hurt to share how I do it. I try to make chunks in the following order: services, modules, single functions from modules, localized global function, instances, custom enums, data variables (tables included), functions, connections, loose code that justs runs. I do what I can to keep my variable naming consistent across my scripts, partially so I can copy and paste lines with ease. I also do what I can to give my variables clear names.
--// Services
local ts = game:GetService("TweenService")
local ds = game:getService("Debris")
local Teams = game:GetService("Teams")
--// Modules
local GameFunctions = require(game.ServerStorage.GameModules.GameFunctions)
local FacilityControl = require(game.ServerStorage.GameModules.FacilityControl)
--// Single functions from modules
local AttackHuman = require(game.ServerStorage.GameModules.CombatFunctions.CombatFunctions).AttackHuman
--// Localized functions
local wait = task.wait
local tbup = table.unpack
--// Instances
local Electrical = workspace.GameMap.Electrical.PowerSystem
local Generators = Electrical.Generators
local Controls = Electrical.Controls
--// Custom enums
local PowerStates = {
On = 1,
Off = 2,
Transition = 3
}
--// Data variables
local Data = {
PressCount = 0,
PowerState = PowerStates.Off
}
local ButtonDeb = false
--// Functions
local function _1984(plr)
plr:Kick("RED SUN")
end
--// Connections
game.Players.PlayerAdded:Connect(_1984)
--// Loose code
print("Hello there.")
I’m not a hardliner about following the above patterns—none of my scripts follow them perfectly nor can they at times—but they are my general guidelines for keeping my scripts organized and tidy.
In my longer scripts, I frequently split sections of code with separators made of a lot of dashes. One section could be all the variables, another for BindableEvent connections, and yet another for ClickDetector connections. How exactly I do it changes with each script and where I find them helpful.
I hate making scripts longer than they need to be, but I will sometimes trade line count reductions for readability by spliting extremely long if statements and tween across multiple lines. This isn’t the best example, but it’s an adequate demonstration:
T1 = ts:Create(
Bar.MovingChargeBar,
TweenInfo.new(0.35,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,-1,true,0),
{BackgroundColor3 = Color3.fromRGB(223,0,0)})
T2 = ts:Create(
Bar,
TweenInfo.new(0.35,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,-1,true,0),
{BackgroundColor3 = Color3.fromRGB(89,0,0)})