Lua Comments
A comment is a note written in code for people to read. Lua ignores comments when it runs a script.
The commented print does not run
The -- at the start of the second line makes it a comment. Only the first print runs, so only its line appears.
print("This line runs")
-- print("This line is hidden")A comment above code prints nothing
The first line is a comment for the reader. Only the print line below it runs.
-- Show the player name
print("Maya")A block comment hides several lines
A block comment starts with --[[ and ends with ]]. The two prints inside it never run.
print("Before the block")
--[[
print("Hidden one")
print("Hidden two")
]]
print("After the block")Example
print("Report start")
--[[
print("Old score", 10)
print("Old level", 2)
]]
print("Report end")Key points
- Line comment: a line comment starts with -- and covers the rest of the line.
- Block comment: a block comment starts with --[[ and ends with ]].
- Ignored: Lua skips comments, so nothing in a comment runs or prints.
- Notes: comments explain what code does for the people reading it.
- Disabling: putting -- at the start of a line turns the whole line into a comment.
In this example, the first print runs and shows Report start. The block comment then hides two whole print lines, so neither of them appears. The last print runs and shows Report end.
Note
a commented-out print stays in the file but never runs.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which marks the start of a block comment in Lua?