Lua crash course part 1
DungeonMaster00 (165)

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:
    • Comments
    • I/O
    • Data types
    • Variables
    • Tables
  • Part 2:
    • Basic operators
    • If statements
    • Loops
    • Functions
  • 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.

More stuff:


Comments

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.

Example:

-- Single line comment

A long comment begins with --[[ and ends with ]].

Example:

--[[
	Long comment.
	Can extend from many lines to part of on line.
	Throws error if not ended with:
]]

Yes, beware. The program will throw an error if the long comment is not ended.


I\O

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 print function

print("hello world!")

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 io.read

io.read()

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 (true/false)
  • number - Any regular number
  • string - A sequence of characters
  • nil - Nothing
  • table - A Lua table (explained later)

You can also see what data type a thing currently is with the type function:

type(<thing it will check the type of>)

Example:

type("this is a string type btw")

Variables

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:

<name> = <value>

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:

variablesAreCool = true
numExample = 791.32
stringExample = "this is an example of".." string concatenation" -- string concatenation combines things and makes them into a new string.
fancyBoolean = 3 != 4 -- this is a condition. They are common in control flow (explained in next part.)

So now you have a bunch of variables and values. Good job!


Tables

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.

Example:

tableExample = {4, "im number 2", false}

You can also have OOP based on tables, which will be explained in a future tutorial.


And so ends Part 1 of my Lua crash course. Tell me how to improve this and of typos and stuff.

You are viewing a single comment. View All
EpicGamer007 (1264)

@nk1rwc,I really liked your D tutorial though!