› Forums › Lua › My First Lua Program: How I Debugged Syntax Errors, File Paths, and Hidden Extensions to Finally Run My Code
- This topic is empty.
-
AuthorPosts
-
June 19, 2026 at 11:51 am #6921
Today I successfully executed my first Lua program. Although the final solution was simple, the journey taught me an important lesson: programming is not just about writing code—it is also about troubleshooting, interpreting error messages, and systematically finding the root cause of problems.
In this post, I will document the complete debugging process from the moment I launched Lua for the first time until I finally saw the correct output appear on the screen.
The Objective
I wanted to create a recursive factorial function in Lua.
The factorial of a number is defined as:
n! = n × (n − 1) × (n − 2) × ... × 1
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
The Lua code I wanted to run was:
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end end print(fact(5))The expected output was:
120
However, reaching that point required solving several different problems.

Attempt #1: Running Windows Commands Inside Lua
I downloaded Lua and launched
lua55.exe. A black window appeared showing:Lua 5.5.0 >
Seeing the black terminal window, I assumed it behaved like Windows Command Prompt.
So I tried running:
lua55.exe fact.lua
Lua immediately responded with:
syntax error near 'fact'
I then tried:
cd %USERPROFILE%\Desktop
and received another syntax error.
At first I thought something was wrong with Lua itself.
The real problem was that I had misunderstood what the window actually was.
I was not inside Windows Command Prompt.
I was inside the Lua interpreter, often called the REPL (Read-Evaluate-Print Loop).
The prompt:
>
expects Lua code, not Windows commands.
Lesson Learned
Always understand the environment you are working in.
- Windows Command Prompt runs operating system commands.
- Lua REPL executes Lua statements.
- A command valid in one environment may be invalid in another.
Attempt #2: Bringing Python Habits Into Lua
Since I had previous experience with Python, my instinct was to write:
def fact(n)
Lua immediately rejected this.
I learned that Lua defines functions differently:
function fact(n)
and uses:
then ... end
to define blocks.
Unlike Python, indentation does not determine scope.
The language structure itself defines where blocks begin and end.
Lesson Learned
Programming concepts transfer between languages, but syntax does not.
Even a simple function definition can look completely different in another language.
Attempt #3: Writing The Recursive Function
After fixing the syntax, I produced the following recursive function:
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end endUnderstanding the recursion was itself an important learning experience.
When calling:
fact(5)
Lua evaluates:
fact(5) = 5 × fact(4) = 5 × (4 × fact(3)) = 5 × (4 × (3 × fact(2))) = 5 × (4 × (3 × (2 × fact(1)))) = 5 × (4 × (3 × (2 × (1 × fact(0))))) = 5 × 4 × 3 × 2 × 1 × 1 = 120
At this point the logic was correct.
The next challenge was executing the code from a file.
Attempt #4: Saving The Script

I saved the script as:
fact.lua
or at least I thought I did.
I then tried loading it from Lua using:
dofile("C:/Users/SDM/Desktop/fact.lua")Lua responded:
cannot open C:/Users/SDM/Desktop/fact.lua: No such file or directory
This error was particularly confusing.
I could clearly see a file named
fact.luaon my Desktop.I verified the path multiple times.
The file appeared to exist.
Yet Lua insisted it did not.
The Investigation
Instead of guessing, I began examining the evidence more carefully.
One clue appeared when hovering over the file in Windows Explorer.
The tooltip showed:
Type: Text Document
That seemed strange.
A true Lua source file should not normally appear as a generic text document.
The real breakthrough came when opening the Save As dialog in Notepad.
Windows displayed:
fact.lua.txt already exists
That single message revealed the entire problem.
The Hidden Extension Problem
Although Windows Explorer displayed:
fact.lua
the actual filename was:
fact.lua.txt
Windows had hidden the final extension.
From Lua’s perspective, the file:
fact.lua
did not exist.
Only:
fact.lua.txt
existed.
Lua was correct.
The error message was accurate.
My assumption about the filename was wrong.
The Solution
To create a true Lua file, I opened Notepad and selected:
Save As
Then I changed:
Save as type: All Files (*.*)
and entered the filename as:
"fact.lua"
including the quotation marks.
This forced Notepad to save the file with exactly that name and prevented Windows from silently appending
.txt.
Success!
After correcting the filename, I returned to Lua and executed:
dofile("C:/Users/SDM/Desktop/fact.lua")This time Lua successfully loaded the script and displayed:
120
After numerous syntax errors, environment misunderstandings, and file extension issues, the program finally worked exactly as intended.
What I Learned
This exercise taught me far more than how to compute factorials.
It taught me how professional debugging works.
- Read error messages carefully.
- Verify assumptions.
- Distinguish between different execution environments.
- Investigate file paths when files cannot be found.
- Be aware that Windows hides extensions by default.
- Use
All Files (*.*)when creating source code files. - Treat every error message as a clue rather than an obstacle.
Most importantly, I learned that programming is often a process of discovering why something does not work before finally making it work.

The final output was only:
120
but the debugging journey provided lessons that will be useful throughout my programming career.
Final Thought
The most valuable part of writing my first Lua program was not the factorial function itself. It was learning how to investigate problems, interpret error messages, test assumptions, and systematically work toward a solution.
That troubleshooting mindset is one of the most important skills any programmer can develop.
-
AuthorPosts
- You must be logged in to reply to this topic.
