11 posts
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
504 posts
Location
Seattle, WA
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
7508 posts
Location
Australia
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.
8543 posts
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/>
11 posts
Posted 17 April 2014 - 05:40 AM
Thanks for the help! It was a simple mistake now thinking about it :)/>
11 posts
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