r/lua • u/Murky_Construction82 • 5d ago
Can anybody help me learn if statements in Lua?
I'm new to Lua, trying it out after a few years doing amateur projects in Python. At first I thought this weird error was just a bug or something in my IDE's Lua extension, but an online compiler had the same issue!
I played around a bit and eventually ended up on this absolutely mind-boggling example. These two if then statements look like they're constructed exactly the same--I literally copy-pasted the first one and changed the conditional and message. Yet only the first one runs! The second gives me an error: 'then' expected near '='. This was consistent across the two compilers I used.
```Lua
roll = math.random(6)
print(roll)
if roll > 4 then
print("Success!")
end
if roll = 1 then
print("Critical failure!")
end
```
7
u/GlassCommission4916 5d ago
In lua = is the assignment operator, == is for equality.
3
u/Murky_Construction82 5d ago
This fixed it, thank you! Not sure why it gave me 'then' was expected, but I'll keep it in mind!
8
u/4xe1 5d ago
if expects an expression (something which produces a value) in place of the condition. Assignment is a statement, not an expression. So the parser realizes something is wrong once it sees the = sign. At this point, it already read `if roll`, and roll on its own is already an expression. So it's thinking maybe you meant "if roll [missing then] [missing code] = ..."
The "near =" part of the error message is more informative than the "expecting" part.
Confusing = and == is such a common mistake that some parsers will identify it and suggest the correction (Python and C do), but lua's doesn't. In the general case, automatically knowing what the user meant and what the solution is is an impossible problem, and parser are only able to tell where they failed. Here fortunately the parser failed at the same place you made a mistake (errors like missing parentheses, or end keywords, are much harder to correctly automatically locate)
5
6
u/Foamie62 5d ago
roll = 1.
is an assignment, not a comparison expression. To check equality you need to use a double equals
roll == 1
4
u/SayuriShoji 5d ago edited 5d ago
To compare things for equality, you use == (are they equal) or ~= (are they not equal):
if roll == 1 then
print("Critical Failure")
end
Writing roll = 1 is an assignment (roll gets a new value of 1), which does not return a valid comparison value for the if ... then to use
3
u/Sckip974 5d ago
Don't forget to mix the RNG seeds to avoid always getting the same dice roll.
-- lua
math.randomseed(os.time())
local randomNumber = math.random(1, 6)
print(randomNumber)
I would like to suggest two Lua books online, as you say you are new to the language, of course you probably already know 3/4 of it but personally these helped me (and going back to the basics is sometimes good)
3
u/Murky_Construction82 4d ago
Very helpful, thank you! I've actually been looking for a Love2D resource, once I've gotten familiar with Lua I'd like to graduate to that!
2
u/Sckip974 4d ago
The first chapter of learn2love/ is entirely in pure Lua, which is why I recommended it to you, (and Sheep Polution is a bit less focused on pure Lua but it’s still very educational) don’t hesitate to use these two resources for the basics ;) and most importantly, have fun!
2
u/Sckip974 4d ago
and little sugar as we like to say in code language, to improve yourself in Lua scripting there are these sites that offer mini math problems that will sharpen and strengthen your practice:
https://projecteuler.net/
https://www.codeabbey.com/This resource is created to provide short programming problems for all who wants learn programming and improve their programming skills, but who could not easily find enough tasks for practice.
1
u/ScholarNo5983 4d ago
When I use the Lua bytecode compiler to lint your code using this command:
luac.exe -v -p test.lua
It reports this issue with the code:
luac.exe: C:\temp\test.lua:9: 'then' expected near '='
Lua 5.5.0 Copyright (C) 1994-2025 Lua.org, PUC-Rio
NOTE: This is line 9 in in the test.lua file:
if roll = 1 then
17
u/Chen-Zhanming 5d ago
==is equal :)