Lua Crash Course part 2
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
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
You also have
elseif for the other conditions after the original
You can have as many
elseifs as you need.
But what if none of the
elseifs are run?
else for that.
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
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.
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
Well we have a
It takes a maximum of three statements.
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.
type is a function. You name it, a ton of thingsa are functions.
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.
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!!!!!!!
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?