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 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)!
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)
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.
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).
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
The world of ROBLOX Lua is at your fingertips! Use it!
 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!