Skip to content
← Back to Community
Lua Crash Course part 2
Profile icon

Lua crash course part 2

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

Other parts of this crash course:

Let the learning begin.


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

Here are the arithmetic operators:
+ is Addition | - is Subtraction | * is Multiplication | / is Division | % is Remainder | ^ is Exponintial

Unlike most languages, you cannot have += and stuff for arithmetic assignment
Here are the comparing operators:
== means Equal to | ~= means Not Equal to | > means Greater than | >= means Greater than or Equal to | < means Less than | <= means Less than or Equal to

Here are the Boolean logic operators:
"and" will make the whole thing evaluate to true if the conditions are true | "or" will make the whole thing evaluate to true if at least one of the conditions are true | "not" reverses the boolean value of something

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.

if <condition/boolean> then -- insert code here end

There's more.

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

if <condition/boolean> then -- some random comments -- indentation is not required in Lua but makes the code more readable. elseif <condition/boolean> then -- try not to think about the fact that there's no end after the first if statement end

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.

if <condition/boolean> then -- code elseif <condition/boolean> then -- code else -- this code is only run if the other if and elseifs are not run end


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.

while <condition/boolean> do -- this code is repeated while the condition or boolean is true end

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

Example for infinite loop:

while true do print("INFINITE LOOP OH NOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!") -- this code is executed forever end

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:
The basic idea of a repeat...until loop

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.

repeat -- code until <condition/boolean>

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.

for <statement executed before loop starts iterating>, <condition that is checked after each iteration>, <statement executed after each iteration> do --code end

You can also nest loops.

break terminates the current loop.

Have fun looping!


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.


function sayhello() print("hello world.") end

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

function paramExample(parameter1, parameter2)

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.


function basicAddition() sum = 1 + 1 return sum end

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:

-- Named arguments local function namedArguments(args) return .. "'s birthday: " .. args.dob end namedArguments{name="Bob", dob="4/1/2000"}


Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon


Profile icon

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?

Profile icon

@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.

Profile icon


ohhhhhhhhhhhhhhhhh thx

Profile icon

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