←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Help!

elliotjarnit's Photo elliotjarnit 19 Nov 2016

So im trying to make a
1: Password file maker(Works)
2: Password protected computer that when you type in a password in a file(password) then it breaks and ends(Not Working)
Here's the code for the first one:
write("New Password: ")
input = read()
if input == "F" then
shell.run("startup")
end
local file = fs.open("password", "w")
file.writeLine(input)
file.close()
Now the one thats not working(Called startup):
write("Enter Password: ")
user = read()
local file = fs.open("password", "r")
file.readLine()
file.close()
if user == file then
print("Correct!")
sleep(2)
shell.run("system")
else
print("Incorrect")
sleep(2)
os.reboot()
end
Quote

supernicejohn's Photo supernicejohn 19 Nov 2016

You would want to save the result of 'file.readLine' into a variable, then compare that instead of comparing a string with a file handle.
The wiki has good support on the fs API
Good luck! ;)
Quote

Mao Zedong's Photo Mao Zedong 19 Nov 2016

write("Enter Password: ")
user = read()
local file = fs.open("password", "r")
local pass = file.readLine()
file.close()
if user == pass then
print("Correct!")
sleep(2)
shell.run("system")
else
print("Incorrect")
sleep(2)
os.reboot()
end
Quote