Notifications Module

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

Btw idrk how to make it into a script like-zone (like this)

, so yeah mb.

First way

Write three ` before and after your code.

For example:

local test = "test"
local test2 = "test2"

image

Second way

Highlight code and press format button inside the Settings thing.
image

Useful link

Sorry for not answering your question, I’m quite busy right now

2 Likes

I think one of the easiest ways and my personal favorite ways to clean code is simply with comments

-- This is a comment.

local Frog = workspace.Frog -- This is a Frog

--[[
This is a
multiple line
comment
]]

Personally I like to do what I call, stylizing my comments, such as:

-- // Modules //
local Modules = X

-- || InfoModules ||
local Info1 = require(Modules.Info1)
local Info2 = require(Modules.Info2)

It just makes it easier to read, as I split it up into sections.

One thing I would also recommend is using modules, such as storing values, this cleans up a lot of space, and helps with readability, E.g:

-- // Main Module //
local module = {}
local Info = require(script.Info)

module.GiveXP= function()
    for _, player in players do
        local leaderstats = player.leaderstats -- Players Info

        leaderstats.XP.Value+= Info.XPIncrease -- Add XP

        if leaderstats.XP.Value >= Info.XPRequirment then -- If user has enough XP, Level up.
            leaderstats.XP.Value -= Info.XPRequirment -- Remove XP
            leaderstats.Level.Value += 1 -- Add Level
        end
    end
end

return module

-- // Info Module //
local Info = {}

Info.XPIncrease = 10
Info.XPRequirment = 200

return Info

These are just some examples, other cases I like to do this with is storing custom types, functions I use a lot, and more.

Hope this helps~
~ Frodev

1 Like

Oh yeah I used to use this in my early days of scripting, but it got a bit too tiring for me to always write them at everything.

1 Like

Thats why Id recommend doing it in sections, or just do it anyways, if you keep it up itll become second nature to do comments

1 Like