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

User account system not working

Started by Weasels, 13 April 2014 - 08:28 PM
Weasels #1
Posted 13 April 2014 - 10:28 PM
Hello, I'm trying to make a user account system that will save usernames and password as .dat files. The creation works just fine, however when I try to login to the user account it keeps saying login:14: attampt to call nil. What am I doing wrong?

Here's the code.


write("Username: ")
user = read()
if fs.exists("active/"..user..".dat") == true then
print("User found.")
sleep(1)
else
print("User not found")
sleep(2)
os.shutdown()
end
write("Password: ")
pass = read("*")
file = fs.open("active/"..user..".dat","r")
text = fs.readAll()
if pass == text then
  print("Welcome "..user.."!")
  sleep(1)
file:close()
else
print("Invalid.")
sleep(2)
shell.run("login")
end
Grim Reaper #2
Posted 14 April 2014 - 02:13 AM
The function you're calling, fs.readAll(), doesn't exist. readAll exists as a function attached to what is called a 'handle' on some file. You get this handle by using fs.open. Since you've already done that on the previous line, you just have to change the 14th line to this:


text = file.readAll()

Also, just a preference but not a necessity, you don't have to do file:close() when using the fs API as the functions don't require a 'self' parameter. So, you could just do:

file.close()
Edited on 14 April 2014 - 12:13 AM
theoriginalbit #3
Posted 14 April 2014 - 02:16 AM
Also, just a preference but not a necessity, you don't have to do file:close() when using the fs API as the functions don't require a 'self' parameter. So, you could just do:
well its more good practise than preference, it is always good practise to close things you open.
Lyqyd #4
Posted 14 April 2014 - 02:57 AM
He was nitpicking the colon vs. period in the closing call, not saying not to close the handle. :P/>
Weasels #5
Posted 17 April 2014 - 05:40 AM
Thanks for the help! It was a simple mistake now thinking about it :)/>
Weasels #6
Posted 17 April 2014 - 10:35 PM
The function you're calling, fs.readAll(), doesn't exist. readAll exists as a function attached to what is called a 'handle' on some file. You get this handle by using fs.open. Since you've already done that on the previous line, you just have to change the 14th line to this:


text = file.readAll()

Also, just a preference but not a necessity, you don't have to do file:close() when using the fs API as the functions don't require a 'self' parameter. So, you could just do:

file.close()

Disregard this message.
Edited on 17 April 2014 - 08:38 PM