Hello, I’ve been scripting for quite a while. I’m very good at scripting, but my only problem is my code’s cleanliness. My code is NOT clean and people have suggested cleaning my script here. Can someone give me tips on how to clean it and make it easy to read and understand?
Script:
local allNotifiers = {}
local players = game:GetService(“Players”)
local debris = game:GetService(“Debris”)
local tweenServ = game:GetService(“TweenService”)
local plr = players.LocalPlayer
local themes = require(script:FindFirstChild(“Themes”))
local darkTheme = require(themes:FindFirstChild(“Dark”))
local lightTheme = require(themes:FindFirstChild(“Light”))
local urgentModule = require(script:FindFirstChild(“Urgent”))
local UIList = require(script:FindFirstChild(“UI”))
local globalRE = script:FindFirstChild(“GlobalRE”)
local notificationUI = UIList:FindFirstChild(“NotificationsUI”)
allNotifiers.NotifierCounter = 0
local min_lifetime_number = 5
local max_lifetime_number = 60
local max_message_string = 70 – Spaces included
local max_notification_amounts = 3 – Limit of the global notifiers
local max_title_string = 25 – Spaces included
function allNotifiers.new(notifierType : string, lifetime : number, name : string, player : Player, global : boolean) – The name will be the title
player = player or plr
global = global or false
assert(allNotifiers.NotifierCounter == max_notification_amounts, "Unable to create notifier, passed the notification counter.")
for _, ui in UIList:GetChildren() do
if ui:IsA("Frame") then
local newNotifier = ui:FindFirstChild(notifierType):Clone()
allNotifiers[name] = newNotifier
allNotifiers.NotifierCounter += 1
assert(not newNotifier, "User put invalid notifier type. Will not replicate.")
assert(lifetime < max_lifetime_number and lifetime < min_lifetime_number, "Invalid lifetime. Maximum is 60 seconds and minimum is 15 seconds.")
task.spawn(function()
if lifetime == -1 then return end
local timeBar = newNotifier.TimeBar
debris:AddItem(newNotifier, lifetime)
tweenServ:Create(timeBar, TweenInfo.new(lifetime, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {["Size"] = UDim2.new(0,0,0,8)}):Play()
allNotifiers[name] = nil
allNotifiers.NotifierCounter -= 1
end)
newNotifier.Parent = notificationUI.NotificationsFrame
newNotifier.Name = name
themes.IncludeTextButtons(false)
themes:SetThemeTo(newNotifier, "Dark")
if global then globalRE:FireAllClients(notificationUI) end
notificationUI.Parent = player:WaitForChild("PlayerGui")
end
end
end
function allNotifiers:ClearArray()
table.clear(allNotifiers)
for _, ui in notificationUI:GetChildren() do
if ui:IsA(“Frame”) then
ui:Destroy()
end
end
end
function allNotifiers:SetMessageTo(message : string, newNotifier)
if #message > max_message_string then newNotifier.Description = string.sub(message, 1, max_message_string) … “…” return end
newNotifier.Description = message
end
function allNotifiers:SetTitleTo(title: string, newNotifier)
if #title > max_title_string then newNotifier.Title = string.sub(title, 1, max_title_string) … “…” return end
newNotifier.Title = title
end
function allNotifiers:AlignTo(position : string, newNotifier)
local notifier = newNotifier.Parent
local possibleAlignments = {
TopCenter = {Position = UDim2.new(.406, 0,.036, 0), VerticalAlignment = Enum.VerticalAlignment.Top, HorizontalAlignment = Enum.HorizontalAlignment.Center},
TopLeft = {Position = UDim2.new(.007, 0,.015, 0), VerticalAlignment = Enum.VerticalAlignment.Top, HorizontalAlignment = Enum.HorizontalAlignment.Left},
TopRight = {Position = UDim2.new(.805, 0,.015, 0), VerticalAlignment = Enum.VerticalAlignment.Top, HorizontalAlignment = Enum.HorizontalAlignment.Right},
BottomCenter = {Position = UDim2.new(.406, 0,.76, 0), VerticalAlignment = Enum.VerticalAlignment.Bottom, HorizontalAlignment = Enum.HorizontalAlignment.Center},
BottomLeft = {Position = UDim2.new(.007, 0,.76, 0), VerticalAlignment = Enum.VerticalAlignment.Bottom, HorizontalAlignment = Enum.HorizontalAlignment.Left},
BottomRight = {Position = UDim2.new(.805, 0,.76, 0), VerticalAlignment = Enum.VerticalAlignment.Bottom, HorizontalAlignment = Enum.HorizontalAlignment.Right},
Center = {Position = UDim2.new(.406, 0,.36, 0), VerticalAlignment = Enum.VerticalAlignment.Center, HorizontalAlignment = Enum.HorizontalAlignment.Center},
CenterLeft = {Position = UDim2.new(.007, 0, .36,0), VerticalAlignment = Enum.VerticalAlignment.Center, HorizontalAlignment = Enum.HorizontalAlignment.Left},
CenterRight = {Position = UDim2.new(.805, 0,.36, 0), VerticalAlignment = Enum.VerticalAlignment.Center, HorizontalAlignment = Enum.HorizontalAlignment.Right}
}
for index, alignments in possibleAlignments do
if position ~= alignments then return end
notifier.Position = alignments.Position
newNotifier.UIListLayout.VerticalAlignment = alignments.VerticalAlignnment
newNotifier.UIListLayout.HorizontalAlignment = alignments.HorizontalAlignment
end
end
return allNotifiers