Returning to... "cluttered" scripts after a break from a project is a turn off... organization tips..?

This isn’t exactly the best category for this, but it’s the best one I can think of…?
Also, this is a bit of a long read, but it shouldn’t be too hard to answer.

Anyways, basically, I work on projects, lose a ton of motivation, work up enough to come back to it a while later, and bam, the scripts are cluttered and ew when I go back to them and it’s a turn off, and I take an even longer break from the project.

So my question is, any tips for organization in scripts?

The reason that cluttered is in quotations in the title is because the scripts themselves aren’t exactly cluttered- or, rather, I can’t exactly think of any way to fix them. As far as I’m aware, all I have to work with is scripts and comments inside the script itself, and that can quickly be a lot on the eyes.

So, for example, let’s say I have a really simple script, but there’s a lot of garbage that goes into making it look pretty when it does the thing I want it to do. The thing I may want to edit is the simple part, but I open the script, and see all the fancy stuff, and I’m like “wait is this the right script? What does this do? There’s so much stuff here?”

So, on top of organization tips, does anyone know if there’s a way to have multi colored comments? So, maybe you could have different colored headers to almost color code the sections in your script to make stuff easier to find?
Or, maybe something like sorting little sections of your script? Like maybe you could group a section of the script into one little thing and like name it, and it’ll have that drop down on the side like functions and if statements and all that have…? So you could see like “Make the NPC move” and you could click the menu and then you’d see the script?
I know those are super specific, but if there is anything like that on roblox, it’d help me a TON.

I know this was super long, my bad, hope you experts can help. Shouldn’t be too hard a question to answer.

  • A consistent order of variables at the top
-- this is an example
local service = game:GetService("Service")

local coolModule = require(pathToModule)

local CONSTANT = 5

local variable = 2

function foo()

end

  • Space stuff out
local myVar = math.abs(foo * bar) / 3
-- is much more readable than
local myVar=math.abs(foo*bar)/3
  • Consistent and specific variable names (don’t randomly switch between PascalCase and camelCase)
  • Strategic use of line breaks to seperate out chunks of logic
local instance1 = Instance.new("Part")
instance1.Transparency = 1
instance1.Parent = workspace

local instance2 = Instance.new("Part")
instance2.Transparency = 1
instance2.Parent = workspace
  • Comments are your friend, it’s fine if a lot of your code is comments
  • Follow DRY (Don’t Repeat Yourself), use functions and loops to make code easier to maintain

You might be able to turn it into a separate function for easier organisation, even if it is used once, and you can also use comments to seperate certain bits of code. (the roblox style guide might’ve said not to do this but I’m not sure).

1 Like
  1. Follow the Single-Responsibility Principle (important for code organization)

  2. Put a comment block at the top of every script defining what the script should do, if the script does multiple independent things split it into two scripts (follow the Single-Responsibility Principle).
    If the script is a module or class make sure to define every function and member in the comment block.

  3. Obviously Well-Named-Variables super important

  4. You can put strategic comments in places to give you quick refreshers of what that code block or function does

  5. Consistency, however you write your code, be consistent. If you use camelCase or snake_case for private variables don’t use PascalCase.
    However you layout your script (services at top, then variables, then functions, then event connections and initialization) be consistent from script to script.

Readability

Do:

local x = 10
local y = 20

Don’t:

local x,y=10,20

Consistency

Do:

local WelcomeMessage = "Hi player"
local ByeMessage = "Bye player"

Don’t:

local welcomeMessage = "Hi player"
local ByeMessage = "Bye player"

Variable naming

Do:

local Players = game:GetService("Players")

local function PlayerAdded(player)
    print(player.Name .. " is a pro")
end

Players.PlayerAdded:Connect(PlayerAdded)

Don’t:

local plr = game:GetService("Players")

local function plrad(p)
    print(p.Name.." is a pro")
end)

plr.PlayerAdded:Connect(plrad)

Spacing

Line breaks

Do:

local InCooldown = false

if not InCooldown then
    InCooldown = true
    
    print("Activated!!")
    task.wait(5)

    InCooldown = false
end

Don’t:

local InCooldown = false
if not InCooldown then
    InCooldown = true
    print("Activated!!") task.wait(5)
    InCooldown = false
end

If you want performance, you shouldn’t do that


Then you should use comments!!

Last but not least: Use :GetService() on all services please

Functions might incur some performance penalty, but unless you’re running code a lot (way more than once per frame), it’s so inconsequential that the organization is much more worth it.

1 Like

When the game is not too heavy I always put them as a function to increase readability! That’s a trick too (maybe)

There is a discussion thread for this lots of usefull tips here from more experienced people.

Personally I would recommend ECS from the discussion thread example.

It helps a lot with this:

In ECS you will label it as a ___ System so you would only need to directly go to that system. More details as explained here.