Learn to Code via Tutorials on Repl.it!

← Back to all posts
Lua Crash Course part 2
DungeonMaster00

Lua crash course part 2

This part will cover operators, control flow, and functions.

Other parts of this crash course:

Let the learning begin.


Operators

Tables were made by taking screenshots of the previews from table generators

Here are the arithmetic operators:

Unlike most languages, you cannot have += and stuff for arithmetic assignment
Here are the comparing operators:

Here are the Boolean logic operators:


If statements

Sometimes, you want code to run only if a condition or boolean is true.

We do that in Lua (and many other programming languages) with if statements.

They end with the keyword end.

There's more.

You also have elseif for the other conditions after the original if.

You can have as many elseifs as you need.

But what if none of the if and elseifs are run?

We have else for that.


Loops

Running code if a certain condition or boolean is true, but sometimes, you want to repeat code.

You can do that with loops.

They are blocks of code that can be repeated as many times as you need or while a condition or boolean is true.

The most basic type of loop is a while loop.

They execute a piece of code while a condition is true.

You can easily do an infinite loop since you have conditions or booleans for the loop.

Example for infinite loop:

However, if a condition or boolean is false when the loop starts, then the whole loop will be skipped over and none of the code in the loop will be run.

So we have a repeat...until loop for that. It will iterate at least once.

Here is the basic idea:

Basically the loop will iterate, then check the condition. If the condition is false, then the loop will iterate again. Otherwise, the loop is terminated.

Both of those are cool and all. But what if we want a more concise version of a while loop?

Well we have a for loop.

It takes a maximum of three statements.

You can also nest loops.

break terminates the current loop.

Have fun looping!


Functions

Functions are an important part of Lua.

They are basically code that can be called upon multiple times.

print is a function. type is a function. You name it, a ton of thingsa are functions.

Example:

You also have parameters, which are like local variables for a function.

You can have as many as you need.

When you call a function and give values to the parameters, those are called arguments.

There is also the return keyword, which will allow it to be used for variables.

Example:

This returns a number value of 2.

And finally, you have named arguments.

I don't completely understand this, but basically, you have to put names for the arguments.

Example from Roblox Developer:


AND SO ENDS PART 2 OF THIS LUA CRASH COURSE! PLEASE REMIND ME OF TYPOS AND THINGS I CAN MENTION PLEASE!!!!!!!

Voters
wetsocks
CarterHefner
EpicGamer007
aabdou
DominicKaderabe
DungeonMaster00
Comments
hotnewtop
JonathanDe13

PERFECT!

Fedhil

can you help me with a question i have? i dont fully understand return since it's so complicated for me, so could you help me with it?

DungeonMaster00

@ananab1221 lets say you call a function and you want to use it as a variable's value. well it calls the function when you assign the function call as a variable value, but when it gets to the return statement, it will assign the return value to the variable and the function stops.

Fedhil

@DungeonMaster00

ohhhhhhhhhhhhhhhhh thx

T3chn0T33n

"Both of those are cool and all. But what if we ant a more concise version of a while loop?" ant = "want"