• 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!

Lua for Beginners

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Okay... So I did not quite like these other beginner tutorials around here as I don't think they really describe things how it really works and not good enough.

So this will be my attempt to create a more understandable tutorial that will cover pretty much everything you will need to know to start off scripting in Lua.

  1. Operators
  2. If-statements
  3. Variables & Scopes
I will first introduce what you're about to learn, and then I will demonstrate how to use it with examples.


Operators

Arithmetic Operators
  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • ^ (exponential)
  • - (negation)

Unlike other programming/scripting languages Lua does not support incremental/decremental operators such as "i++" or "i--".

The following code will not run in Lua:
Lua:
i = 10

i++; -- This is not allowed in Lua, and will return an error in your console.
i = i + 1 -- This is the valid version of above.

i += 5 -- This is not allowed in Lua, and will return an error in your console.
i = i + 5 -- This is the valid version of above.
We will look further into variables soon, so don't worry if you didn't catch!


I'll go through each arithmetic operator in below script:
Lua:
a = 2 + 5 -- The result is 7, regular addition method.
a = -4 + 6 -- Result is 2 (Would be same if it was "6 + -4").
a = 10 - 4 -- Result is 6, regular subtraction method.
a = 4 - 7 -- Result is -3
a = 5 * 3 -- Result is 15, regular multiplication method.
a = -3 * 3 -- Result is -9
a = 20 / 4 -- Result is 5, regular division method.
a = 20 / -5 -- Result is -4
a = 5 ^ 3 -- Result is 125 (5 * 5 * 5), regular exponential method
a = -10 ^ 2 -- Result is -100
a = 10 ^ -2 -- Result is 0.01
a = -4 -- Negation (note the "-" token).


Relational Operators
  • == (a is equal to b)
  • ~= (a is not equal to b)
  • < (a is less than b)
  • > (a is greater than b)
  • <= (a is less or equal to b)
  • >= (a is greater or equal to b)

A statement is when you have 2 values and compare them to each other using either of the listed operators, the result is a boolean value, either true or false depending on whether the statement was succeeded or not. Here is an example of the usage:
Lua:
4 == 6 -- returns false, because obviously 4 does not equal to 6
4 == 4 -- returns true, 4 equals to 4
3 ~= 5 -- returns true, because 4 does not equal to 6 (which is what we want)
3 ~= 3 -- returns false, 4 equals to 4 (we don't want them to be equal)
7 < 3 -- returns false, 7 is not less than 3
2 < 3 -- returns true, 2 is less than 3
3 < 3 -- returns false, 3 is not less than 3
8 > 4 -- returns true, 8 is greater than 4
4 > 4 -- returns false, 4 is not greater than 4
2 > 4 -- returns false, 2 is not greater than 4
6 <= 8 -- returns true, 6 is not equal, but less than 8
8 <= 8 -- returns true, 8 is not less, but equal to 8
12 <= 8 -- returns false, 12 is not less or equal to 8
1 >= 5 -- returns false, 1 is not greater or equal to 5
9 >= 5 -- returns true, 9 is not equal, but greater than 5
5 >= 5 -- returns true, 5 is not greater, but equal to 5

Note: Be cautious for not mixing "==" with "="

Relational operators are used in if-statements, which we will go through in this tutorial. They could also be used for different purposes, but that is irrelevant for now.

Logical Operators
  • and
  • or
  • not

Logical operators combines with relational operators.
Lua:
10 < 5 and 3 < 9
-- to make it easy for you, we'll convert them one after one...
-- 10 < 5, is false so now we have:
false and 3 < 9
-- 3 < 9 is true, so now we have:
false and true
-- Because we have "false and true", this whole statement will be false. Because the statement wants them BOTH to be true.
false
-- However, if we do the complete same thing, just with "or" it will look like this instead:
10 < 5 or 3 < 9 -- Doing the same as we did before, I'll skip the steps you already know.
false or true -- As before, we have the same values, except this time it's "or" instead of "and"
-- Now it's "false or true", which means it will only want one of them to be true for the statement to go through, "either a is true or b is true" while the and statement says "a is true and b is true". Our result will be:
true
-- To repeat, the reason of this is because it wants either one of them to be true. So if one is false, and one is true it's a true statement, if they both were true it's a true statement, however if they both where false it would be a false statement.

10 < 5 and not 3 < 9
-- The "not" operator simply inverts the statement after it. If "3 < 9" is true, the "not" operator will turn it into false, and vice versa. So the above statement equals to:
10 < 5 and 3 >= 9
-- To slow it down a bit, we know that 3 < 9 is true, so we can replace it:
10 < 5 and not true 
-- Now the "not" operator will invert "true" into "false" simply, and now we'd have:
10 < 5 and false
-- 10 < 5 is false so we would now have:
false and false
-- The end result would be:
false


If-statements

The structure of an if-statement is the following:
Lua:
if statement then -- statement opens with "if", and ends with "then"
   -- here is the code that will be run if statement returns true
end -- close the if-statement with "end"

An if-statement will go through only if the statement returns "true"! If it is not true, then the block-code (code within the if-statement) will not be ran!
Lua:
if true then
   -- block-code
end

Now as that statement is true already, it will always run. However if it was "false" it would never run. An if-statement normally contains relational and logical operators.
Lua:
if 14 < 10 then
   -- 14 < 10 is false, therefore this code will not run.
end

if 3 < 7 then
   -- 3 < 7 is true, the code will run.
end

if 3 == 5 or not 4 ~= 4 then
    -- 3 == 5 is false, 4 ~= 4 is false, however "not" will inverse it, so we will have "false or true". Which means the result will be true and this code will run.
end

For more complicated statements the use of parenthesis could be very handy.


Tutorial is incomplete and will be filled out as quick as possible!

For further learning check out the tutorials linked in my signature below.
 
Last edited:
I'll admit it isn't so much for beginners, but for others coming from other languages it sure helps a lot.
 
This is not for beginners ? This is the absolute basics of Lua, how can it not be ?
 
People who are just starting have no idea what i++; means, so your first section has some useful things but it still leaves out quite a bit. You could also simplify the logical operators by explaining first hand what each one is.
For instance the And logical operator means: Two sides of the equation must be the same, either True, or False much like this example:
Lua:
5 < 10 and 3 < 9
--The 5 is less than 10, and the 3 is less than 9, so both sides of the equation are true.

if 5 < 10 and 3 < 9 then
--This is saying if the entire equation as seen above is true, then do a specific action.
end
 
I know how to do this. It's too hard to explain for a noob how to learn... The absolute perfect way of doing it is using animated images... I'll try do that ^.^
 
He just said that i++ as an increment value wont work in lua, as maybe ppl can walkthrough some other languages using the i++, thinking it might be use in lua,
Other wise , it is nice to waste your time on teaching ppl, and ye it is for absulute beginners. :p
 
Good Tutorial Colandus.
Of course probably someone that has no idea about a lua file even exists wont understand a lot...
Anyway, I do not know a lot about scripting but I know some stuff and this is useful and I think it will help.
 
did not understand anything and did not learn anything
:S

That's because you're the forums biggest retard.

"IF YOU HELP ME, I WILL HELP YOU, ME REAL FRIND CYKOTITAN" oh please SHUT THE FUCK UP..
 
Back
Top