Your First Program

Welcome to the Lua course! Lua is a small, fast programming language. It was created in 1993 at PUC-Rio, a university in Rio de Janeiro, Brazil, by Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemar Celes. Its name is the Portuguese word for “moon”.

Lua was built to live inside other programs. Big programs, such as games, are usually written in other languages, and their creators add Lua to them, so that they can describe in a few lines of Lua how things behave: what a character does, what happens when you open a door, what a button shows. Because Lua is small and easy to add to a program, you'll find it in many games. Games on Roblox are programmed in Luau, a language that grew out of Lua. Add-ons for World of Warcraft, which players write themselves, are written in Lua, and so are games made with the LÖVE and Defold game engines. Outside of games, you'll find Lua in the text editor Neovim and in the database Redis, for example.

This course teaches standard Lua, version 5.4. Roblox's Luau grew out of an older version of Lua and added some things of its own. Most of what you learn here works in it too, and the last lesson of the course shows the differences.

What's ahead? You'll learn to store values in variables, make decisions with conditions and repeat work with loops. You'll keep lists of things in tables, write your own functions, use tables as dictionaries and work with text. Then come two of Lua's specialties: objects built with metatables, and coroutines, which are functions that can pause and continue later. In the final project, you'll put it all together into a simulation of a whole season on a farm.

Which farm? Sunflower Farm, which belongs to Emma. She grows carrots, potatoes, pumpkins, strawberries and wheat, and she keeps hens for their eggs, Bella the cow for her milk and Rex the dog for company. She buys seeds, sells her crops at the market for coins and keeps an eye on the weather. Her neighbor Grandpa Joe and Hugo the trader drop by now and then. All the examples and exercises in this course come from her farm.

But first, your very first program. A program is a series of commands that the computer carries out one after another, from top to bottom. This one has just one command, and it prints a greeting:

print("Hello, Lua!")

This prints:

Hello, Lua!

What the parts mean:

  • print is a function: a command with a name that does something. print prints what you give it into the output, the black pane where you see what your program printed (on a phone, under Output). Each print prints one line.
  • The parentheses ( ) after the name hold what the function should work with. Writing the name of a function followed by parentheses is called calling the function.
  • "Hello, Lua!" is a text. Programmers call a text a string, as in a string of characters. The double quotes show Lua where the text starts and where it ends. They aren't printed.

Lua treats uppercase and lowercase letters as different, so Print("Hello, Lua!") won't work: Lua doesn't know any Print, and the program stops with a red error message below the output. Don't worry when that happens: check the line, fix it and run the program again. Later in this lesson, you'll learn to read error messages.

One more thing: the programs on this site can only write to the output. They can't read what you type on the keyboard, so everything they work with is written right in the program.

Instructions

The editor contains the program from the example. Click Run below the editor and look at the output (on a phone, under Output). The first run may take a moment, while Lua loads.

Then change the text in the quotes, so that the program welcomes you to Emma's farm:

Welcome to Sunflower Farm!

Emma has a new helper on the farm: you. Below the first print, add a second one that prints Farm helper: and your name on the next line, for example:

Welcome to Sunflower Farm!
Farm helper: Anna

Put the whole text, your name included, inside the quotes.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/7