Basic Optimisation

• Not recommended for beginners.

This is a basic tutorial to help you make your code as optimised as possible. It will cover the most commonly encountered situations.

I apologise for my code formatting, Tab currently does not work in the text editor, so I will have make do with nothing until I find something better.

Indexing [1]

Indexing isn't too much of a problem - the differences optimising it will make are so small humans probably won't notice it. But, it is still an improvement, and all improvements count.

``````local Vector1 = Vector3.new(0,1,0)
local Vector2 = Vector3.new(0,2,0)
``````

When you index something, the computer "grabs" whatever you told it to. Of course, your arm would eventually get tired if you are grabbing something a number of time (let's imagine you put it back when you're done with it).

See how I'm indexing `Vector3.new` twice? Well, we could decrease the effect on the computer by simply indexing it once. To accomplish this, we simply define Vector3.new as a variable.

``````local Vector3New = Vector3.new -- Did you know you can index normal functions without parentheses?
``````

Then, we just call it like usual:

``````local Vector3New = Vector3.new
local Vector1 = Vector3New(0,1,0)
local Vector2 = Vector3New(0,2,0)
``````

You can even define the entire library (for indexing global libraries can also weaken that arm):

``````local Vector3Library = Vector3
local Vector3New = Vector3Library.new
``````

This is relevant to (and based around) objects and tables.

There we go, you have no released a small amount of stress from your computer (or whatever else is doing the computing)!

Functions

This is a bit self explanatory, but instead of running more than one instances of the same code, you can just stick the code in a function and call it whenever you need it!

``````for i = 1,10 do
print(i)
end

for i = 1,10 do
print(i)
end
``````

See how you're doing this twice? Why not stick it in a function!

``````function printToTen()
for i = 1,10 do
print(i)
end
end

printToTen()
printToTen()
``````

You can even define parameters to it, so it can do different things;

``````function printToNumber(startNumber,endNumber)
for i = startNumber,endNumber do
print(i)
end
end

printToNumber(1,20)
printToNumber(12,60)
``````

Formatting

One thing to always do is keep your formatting clean (see this tutorial for a start). It will help you - and others - to read through your code quickly and easily.

Variables [1]

When defining variables, people usually start off by simply defining them;

``````myNumber = 1
``````

It can be accessed from anywhere in the code, even before it has been defined (this will return an error if you try and index it before it's been defined, time-wise).
It also causes a small amount of inefficiency, so you may want to eradicate it.

To do this, simply define it locally;

``````local myNumber = 1
``````

Note that with locals, you won't be able to access it out of its 'tier' (as I like to call them).

Good example:

``````local myNum = 1
if true then
myNum = 2
end
print(myNum)
``````

``````if true then
local myNum = 2
end
print(myNum) -- throws an error, because it has only been defined withing the if statement
``````

USE EVERYTHING

Use everything you possibly can - so that you don't have to waste time debugging and making redundant code.
Two very helpful examples of what is available: Scripting Helpers and the ROBLOX Wiki!

The world of ROBLOX Lua is at your fingertips! Use it!

[1] This is considered micro-optimisation, and should only be used if performance is flagging.

Hopefully this short tutorial helped you make everything in your code more efficient. If you have any questions, or comments, please ask/discuss them below.

Good luck on your scripting journey!
~TDP

• There is a difference between optimization and micro-optimization; and
the first example you provided obviously falls under the micro-optimization category.

Optimization is the act of [re]writing code to be faster, or use less resources from the computer;
Micro-optimization is when you try and squeeze every little bit of performance out of a computer program.

While writing efficient code is good, early optimizations and micro-optimizations are bad for a project's structure, and I am not sure teaching people how to optimize or micro-optimize their code is such a good idea. At least not without also teaching them when and when not to spend time optimizing.

The rule is that one should only care about performances when writing time critical code.
Not that optimizing the rest of a program is bad; but you shouldn't waste time optimizing your program unless you have performance issues with it.

Micro-optimizations are also generally useless. As mentioned before, the difference micro-optimizations make are so incredibly small that there is no way a human could notice it. Unless you are writing a program for NASA or something, where performances are always an issue, you probably shouldn't ever waste time with micro-optimizations. Micro-optimizations are the root of all evil.

• @TheDeadlyPanther You can copy and paste a tab. Just saying.

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.