Need tips for making Scripts Shorter

Hello Developer Forum!
I would like to hear some tips about how to make my scripts look more professional and less rookie-like (or more appealing to the Scripter’s Eye)

I have tried looking for tips at https://devforum.roblox.com/t/what-were-some-of-your-mistakes-as-a-game-developer-in-2019/424736/19 but I still don’t really get it.

I tried modules and for loops, but are there any other options?

Would be grateful for some feedback.

(Thinking of moving this to a different category)

Structurizing your scripts into can reduce the chances of redefining variables that don’t need to be and having a clean overview of your script.

Services → Module → Imports → Constants → Variables → Functions

2 Likes

Although a shorter might provide readability, it is subjective to your comprehension of the code. You might stick to the longer methods, but try some theory around the code and perceive code as chess move orders(that’s how I think usually).

1 Like

So like this?

--Services--
etc...
--Modules--
etc...
--Imports--
etc...
--Constants--
etc...
--Variables--
etc...
--Functions--
etc...

Use module scripts. This stops you from repeating the same code again and again. Also, use comments so other scripters reading your code understand. Use indentation/whitespace, and make sure to use Variables instead of repeating something like

local Part = workspace.Part
-- Better than:
workspace.Part

Yep you got it! You can see more about the Roblox Style Guide here Roblox Lua Style guide

Are for loops a good option? Or is there something much better?

Using loops can shorten down your code significantly for example:

local Array = {"Dogs", "Parrots", "Cats", "Mice", "Goldfish"}

-- Anyone who doesnt use loops would do
print(Array[1], Array[2], Array[3], Array[4], Array[5]

-- Using a loop
for index, pet in pairs(Array) do
    print(pet)
end

Using loops can help you “automate” your scripts

Do functions help shorten or should I use module functions.

  local module {}
  function testing ()
        print("module successfully required")
  end)

Script without module, given the option to close the function

local function testing ()
        print("successfully called function")
end

testing()

I just merge the variables to make the script look shorter.
for example:

local hum, root, head, torso = char.Humanoid, char.HumanoidRootPart, char.Head, char.Torso
local CFNEW, V3NEW, CFANG, RAD, DEG, PI = CFrame.new, Vector3.new, CFrame.Angles, math.rad, math.deg, math.pi

Unfortunately, that shortening is only preference and sometimes throws a few scripters off for the unconventional(but not really, it’s just not regularly used) naming.

Structurize your scripts,
If you use an instance multiple times, make a local for it
use loops wherever you repeat an operation for multiple parts

Unconventional? Shortening variables like that simply makes the script harder to maintain. It’s bad practice. If you want to obfuscate your code then it’s just not the best way to go about it.

I do enjoy the nice way you put it though.

2 Likes

I was just looking for that word. I couldn’t get it out today. :grin:

1 Like

I don’t think of any better tips than using functions and parameters. Putting parameters in functions helps a TONS to reduce your script lines!