How do i tidy a script?

I’ve always seen people complain about codes having tons of Local lines at the start, my question is, is there a way to “tidy” it up? i’ve also seen my own scripts where the same thing happens, though if i have many things i’ll just divide it by different categories, heres an example;

-- Services
Local Xservice = game:GetService("Xservice")
Local Xservice = game:GetService("Xservice")
Local Xservice = game:GetService("Xservice")
Local Xservice = game:GetService("Xservice")


-- Things to spawn
Local Xpart = game:FindFirstChild("Xpart")
Local Xpart = game:FindFirstChild("Xpart")
Local Xpart = game:FindFirstChild("Xpart")
Local Xpart = game:FindFirstChild("Xpart")
...

Is there a way to tidy this up? I’ve thought of maybe just placing these inside functions that’ll use them, but i don’t really know what else i could do.

3 Likes

Use loop to spawn parts, for example have table with part positions, then when needed clone those parts and place them in those positions

3 Likes

Thats actually not bad advice, though i’m talking more about having everything in there just looking a little better since a lot of the times it’s just “local x local x local x local x…”

1 Like

I would take whitespace into account, and give your variables unique names.

Spacing your code out will allow you to read in sections:

local x_part = workspace.Xpart
                                       -- whitespace
local y_part = workspace.Ypart
local z_part = workspace.Zpart
                                      -- whitespace
local xz_part = workspace.XZpart

Otherwise it depends on what you’re using these variables for. If you’re going to use the same functions repetitively, then I suggest storing them in a ModuleScript.

3 Likes

You could use a service module, in other words a module which holds all the GetService requests in a table, then each script just needs to require the single module.

For specific references to parts, unless you want to find them dynamically each time (not recommended if doing this a lot) you would need to store them in a separate module again.

( I don’t do either of these btw, unless I deliberately want to allow references to be available to other scripts. Plus I like having a reminder of what the script actually needs references for when I come back to it after a long break)

2 Likes