Skip to content
Lesson 1 of 18

Step 1 of 5 · Reading · ~3 min

Learn

First Steps

Hello, Lua!

Lua was created in 1993 at PUC-Rio in Brazil to solve one problem: give a large program a scripting layer without dragging in a large language. Every other design decision follows from that. The interpreter is a few hundred kilobytes. The standard library is small on purpose. There is exactly one container type. Lua fits in whatever space is left over.

You are running Lua whenever you script a Roblox game, configure Neovim, write a World of Warcraft addon, handle a request in OpenResty, or send EVAL to Redis.

What does a Lua program look like?

A Lua file is a chunk — a list of statements executed top to bottom. There is no main, no class to wrap things in, no import ceremony. The first line of the file is the first line that runs.

print("Hello, Lua!")

--> Hello, Lua!

print writes its arguments and then a newline. That newline is automatic; you never append one yourself.

What can you hand to print?

print calls tostring on every argument, so it accepts any value at all.

print(42)
print(3.5)
print(true)
print(nil)

--> 42
--> 3.5
--> true
--> nil

Notice that nil prints as the word nil rather than as nothing. That detail matters later, when a missing value travels quietly through your program instead of crashing it.

The comma trap

print accepts any number of arguments and separates them with a tab, not a space.

You writeWhat lands on stdout
print("Hello, Lua!")Hello, Lua!
print("Hello,", "Lua!")Hello, + tab + Lua!

✓ one string, one argument

print("Hello, Lua!")

✗ two arguments, joined by a tab

print("Hello,", "Lua!")

On screen a tab and a space look identical, so the second version looks correct and still fails a byte-exact check. Whenever a grader says your output is wrong and your eyes say it is right, go looking for a comma inside print.

Quotes, comments, semicolons

Single and double quotes are interchangeable'hi' and "hi" build the identical string. Pick one style; switch only to avoid escaping.

print('She said "hi"')

--> She said "hi"

Comments start with -- and run to the end of the line. Block comments sit between --[[ and ]].

-- a one-line comment
--[[ a comment that
     spans several lines ]]
print("still runs")

Semicolons are legal but optional, and idiomatic Lua leaves them out. A newline ends a statement.

How your work is checked

Every exercise in this course feeds your chunk a fixed input on stdin and compares stdout against an expected string. Capitalisation counts. A tab where a space belongs counts. A blank line in the middle counts, and so does a missing one. What does not count is whitespace trailing off the very end: a stray space after the last character, or one extra blank line at the bottom, is trimmed before the comparison and still passes. Everything before that has to match, which is exactly why the comma trap is worth learning on the first page.

Your exercise

Print Hello wants one line of output and nothing else: Hello, Lua! — capital H, capital L, a comma, a single space, an exclamation mark.

The mistake the grader will catch here is the comma trap. print("Hello,", "Lua!") puts a tab where the test expects a space, and the failure looks like no failure at all. Pass a single string.

Up nextVariables and AssignmentFirst Steps

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…