• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

How to tab Lua scripts

Limos

Senator
Premium User
Joined
Jun 7, 2010
Messages
10,013
Solutions
8
Reaction score
3,055
Location
Netherlands
Since there are a lot of people who don't understand how to tab, I decided to make a little tutorial about it and I hope it's clear enough like this.

fotolia_2655886_XS.jpg


Why is it important to tab scripts?
Tabbed scripts more easy to read and understand.
Within a few seconds people can find a missing end in a tabbed script, in a script that is not tabbed this can take hours (people will probable just tab the script instead).
Same goes for other problems. With tabbed scripts it's really clear which part belongs to what and this is needed to solve problems with scripts.
Also for yourself, you won't have problems with missing ends, to much ends or Lua functions in the wrong place if you tab your scripts, because in a few seconds you can see what's wrong.


There are several things in Lua that need an end to close it, for that you have to use tabs.
Most used ones in OT scripts are function, if and for, they all close with the word end.
There is also while (end) and repeat (until).

I will use function onUse, if and for as example.


I will start with the function.
After you wrote the function, in the next line you press Tab (above Caps Lock) before you write a new line.
Under that the end should be right under the function.

Now there will be a space between the function and the end that closes the function.
You can see the space as "property" of the function, you can't write anything in that space untill the end that closes it.
The green stripe here is the space of the function.

bumk4d.png




Like function, also an if statement needs an end to close it.
So after you write a line that starts with if, next line you need to tab again.
The yellow stripe here is the space of the if statement.

W3DDlC.png




After an if statement, you can write else, for something that will happen if the if statement is not true.
So else is a part of an if statement and should be in the space of the if statement. The rest still can't be in that space.

znZwH-.png




Same goes for elseif, which works similar to else except that it's more like an if statement.
I also added a magic effect to show you have to write anything right under it, as long as it's not in the space of an if statement or the function.

CgfJOP.png




When function or an if statement closes with end, everything under it is not in their space anymore, so you have to write it right under the end without making a new tab.
The dark yellow stripe here is the space of the second if statement.

1wlz7H.png




When you have an if statement inside an if statement, it will be the same thing, after the second if statement, you press tab again so the second if statement will have it's space inside the first if statement.
The orange stripe here is space of the second if statement (third in total).

NEqHEG.png



A for loop also closes like function and if with the word end.
It is often used in combination with tables, but if you do it like this it will just repeat everything till the end 3x (1,2,3 = 3x).
Also here it's important to see here where it ends, because if you add for example the textmessage above the end, you will get this message 3x.
The red stripe here is the space of the fourth if statement and the purple stripe the space of the for loop.

x9Ohg.png


If you have questions or things still aren't clear enough, let me know.
 
Last edited:
good tutorial mate.

This practice is so common that there exists programming languages where it is mandatory like Python.
The term widely used in the programming world is "indentation" or simply "indent the code".
 
Lua:
function correct
    if then
  
    end
  
    if then
        if then
        end
    end
end

function wrong
if then
end
if then
if then
end
end
end
 
Missing () at the end of function name.
Lua:
function correct() -- here
    if then

    end

    if then
        if then
        end
    end
end
Lua:
input:2: unexpected symbol near 'then'

my friend, code is damaged :rolleyes:
 
Back
Top