r/lua • u/Public_Network9220 • Mar 30 '26
Beginner lua coder here, genuinely what did i even do here? Atleast it does something
I made this as a distraction, wasnt thinking too much about it till i started wondering what does it even do. It works atleast
34
Upvotes
26
u/hougaard Mar 30 '26
10/10/11 is a math expression, not a date.
11
u/fatboychummy Mar 31 '26
Put quotes around
10/10/11. As well, you are currently requesting user input twice.Think of parenthesis as the cue to Lua to actually run another sequence of code. When you do
birth == io.read(), it is actually trying to get user input a second time.What you likely want here is something like so:
5
u/iangauss Mar 31 '26
A breakdown for each stuff, with easier term, hopefully not becoming bloated explanation and backfired instead.
io.read() is a function that will wait for user input, and then returns whatever the user inputs.
you wrote
local read = io.read()so it will run that io.read() function, and the user input will be saved to a variable calledreadNow you had the info you needed from the user on the
readvariable.You wrote
local birth = 10/10/11. Now here's the thing,birthwon't save "10/10/11" but 1/11 or 0.0909. It's because if you wrote10/10/11and not"10/10/11", it will become a mathematical expression, not a text (or string in programming term). This is same as if you writelocal text = 10+10, that text variable won't be a "10+10" text, but 20. So if you want to assign a string (text) value, you need to use " ".Now in the conditional statement, you wrote
if read and birth == io.read() and 10/10/11 thenFirst, say if you have local variable
x. If you doif x thenit will do one of 2 things. If x is a boolean (true or false value) then it will simply enters if x is true, and not if x is false. But if x is not a boolean, it will check if the value of x even existed. So if x is a number or a string or anything, if it exist it will enters. But if x is nothing, a null or nil. It won't enters.Second, on
birth == io.read(). You want to check if what the user inputs is the same as what you setbirthis. The thing is, you already did that above. You've savedreadabove l, if you do io.read() again you do the function again but this time the value is only used for the comparison with birth, so thereadyou have is now useless. You should comparebirthtoread, so justif birth == read then. (Or you could still just do the io.read() directly when comparing birth, but for now, putting io.read() value on its own variable is more ideal)So
if read and birth == io.read() and 10/10/11 thenwill end up becoming most likely asif true and birth == io.read() and true then, so it end up equal toif birth == io.read() then. Which really should beif birth == read thensince you alreafy hadread2
u/Cootshk Mar 31 '26
10/10/11 gets converted into (10/10)/11 or 1/11
The if statement checks
readandbirth == io.readand10/10/11readwill always be true as io.read can never return false or nilbirth == io.read() will always be false because a string of text is never equal to the number 1/11
And 1/11 is a true value
Anding those together, the if statement will always be false
2
u/Relevant_South_1842 Mar 30 '26
What is your question?
1
u/TomatoCo Mar 30 '26
It's surprising this works! io.read() reads as a String but your birth variable there is a number equivalent to 1/11.
I'd expect it to read from your input twice, once on line 6, and again on line 9. The first read is then checked on line 9 where, because it contains anything, is truthy. Then you check that birth is equal to the second thing you read, which I'm surprised passes. Finally, you check the value 10/10/11, which is 0.090909etcetera. This isn't false or nil, so it's also true, so this part also passes the if statement.
Unless of course by "it works" you mean it runs but doesn't give you the expected output.
1
u/fish_Vending Mar 31 '26
.09 repeating... Your doing division.