Lua crash course part 1
Before we start
Lua is a lightweight scripting language that still has many uses today.
Roblox Studio uses it and even has special APIs for they're scripts.
It is also very suitable for a beginner's language.
This is the contents:
- Part 1:
- Data types
- Part 2:
- Basic operators
- If statements
- Part 3:
- OOP in Lua
- Exception handling
- Sources used
Lua Crash Course part 1
In this tutorial, I will explain the basics of the Lua programming language.
I will try to be clear and concise with everything so that you can easily learn this.
What are comments?
Comments are part of a program that's not run.
A single line comment starts with
-- and will make the rest of the line not run.
A long comment begins with
--[[ and ends with
Yes, beware. The program will throw an error if the long comment is not ended.
The first program you will probably write for any given programming language is something that writes something similar to "hello world" in the console.
This is extremely simple in Lua. You use the
This is the same as Python and Swift.
We have done output to the console. Now let's do console input.
We accomplish that with
Unlike other programming languages, you cannot give a prompt string in the parenthesis
Basic Data types
Lua is dynamically typed.
Because of that, you can change the variable type anytime.
But the values have seprate types. Here are a few basic ones:
boolean- A boolean value (
number- Any regular number
string- A sequence of characters
table- A Lua table (explained later)
You can also see what data type a thing currently is with the
Variables are an essential part of Lua (and many other programming languages.)
Well what are they?
Think of them as values stored under a name.
They have the following syntax:
Since lua is dynamically typed, variable types can change at any time.
And also, as you guessed, you can use the
type function with variables too.
Example for declaring variables:
So now you have a bunch of variables and values. Good job!
What are tables?
Tables are basically your way to make arrays in Lua.
Unlike most other programming languages, tables have indices starting at 1.
If the stuff in there has a name, then it doesn't have an index.
You can also have OOP based on tables, which will be explained in a future tutorial.