This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dfrankcom's profile picture

IF Statement Not Working

Started by dfrankcom, 21 September 2012 - 10:54 PM
dfrankcom #1
Posted 22 September 2012 - 12:54 AM
Hi Guys,
I am new to the forum and am relatively new to ComputerCraft. I have been working on a script that takes the users input and displays the corresponding block title for the ID that they entered. My code is as follows:

write("What item ID?\n")
local ID = read()
if ID == 1 then
  write("Stone Selected\n")
end
For some reason the if statement will not work. By this I mean that it just takes the user's input and then does nothing. Nothing is displayed, the code just ends. I have tried everything to the extend of my knowledge and am completely lost. I get no errors, it just does not work when it gets to that section of the code. Please help!
Thanks,
Daniel
Lyqyd #2
Posted 22 September 2012 - 01:02 AM
The function read() returns a string, which you are then comparing against a number. You should either compare the ID against a string (if ID == "1" then) or tonumber() the results of the read() call.
dfrankcom #3
Posted 22 September 2012 - 01:05 AM
Awesome!
It worked. Thank you sooo much!