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

Door lock giving me problems.

Started by xxbryanjxx, 09 January 2015 - 10:45 PM
xxbryanjxx #1
Posted 09 January 2015 - 11:45 PM
I assumed this type of program would be easy to write.
The user inputs a password, (X) and the script will compare the password to the pre-inserted password.(P)
but instead if in the password (5562) it just tells me the password is incorrect.

p = 5562
i = 0



textutils.slowPrint("Password? ")
x = read()

if x == p then
rs.setOutput("back",true)
sleep(5)
rs.setOutput("back",false)

else
textutils.slowPrint("password incorrect")

end
Cranium #2
Posted 10 January 2015 - 12:33 AM
read() returns a string, and you're likely getting an error trying to compare string to number. Try either putting the 5562 in quotes when defining p, or using tonumber to redefine x as it comes in:
x = tonumber(read())
Those are the easiest ways, to do what you want, but far from the most elegant.
Edited on 09 January 2015 - 11:33 PM
xxbryanjxx #3
Posted 10 January 2015 - 12:53 AM
If i instead turn the number into a word, and kept read() would i be able to enter a word as the password?
Bomb Bloke #4
Posted 10 January 2015 - 04:13 AM
If you turned your stored password into a string:

p = "5562"

… then you could directly compare it to the value returned by read() (which is also a string).

http://lua-users.org/wiki/LuaTypesTutorial