695 posts
Location
In my basement.
Posted 30 March 2013 - 05:23 AM
I am trying to make a thing that changes a password. I have it running but when i try to change the password i get an error; test:885: Attemp to call nil. Line 885 is in this code snippet line 21.
Anyways here's the code:
function changepass()
term.clear()
term.setCursorPos(1,1)
write("Username: ")
local usrName = read()
write("Old password: ")
local oldpass = read("*")
write("New password: ")
local newpass = read("*")
if not fs.exists("users/"..usrName) then
term.clear()
term.setCursorPos(1,1)
print("No such username!")
sleep(1)
term.clear()
acc()
elseif fs.exists("users/"..usrName) then
local fileData = {}
local file = fs.open("users/"..usrName,"a")
repeat
table.insert(fileData, line)
line = file.readLine()
until line == nil -- readLine()
file.close()
local oldpassFromFile = fileData[1]
if pass == oldpassFromFile then
file.clear()
file.writeLine(newpass)
file.close()
term.clear()
term.setCursorPos(1,1)
print("Changed!")
sleep(1)
term.clear()
acc()
else
term.clear()
term.setCursorPos(1,1)
print("Failure!")
sleep(1)
term.clear()
acc()
end
end
end
2088 posts
Location
South Africa
Posted 30 March 2013 - 05:55 AM
Looks like it's the part with table.insert( fileData, line )
You're trying to insert the variable 'line' into the table 'fileData' when it hasn't been defined yet.
Try this:
local file = fs.open("users/"..usrName,"a")
local line = file.readLine()
while line do
table.insert(fileData, line)
line = file.readLine()
end
695 posts
Location
In my basement.
Posted 30 March 2013 - 06:24 AM
Thanks, I will
695 posts
Location
In my basement.
Posted 30 March 2013 - 06:41 AM
Well, it doesn't work
2088 posts
Location
South Africa
Posted 30 March 2013 - 06:44 AM
Change
local file = fs.open("users/"..usrName,"a")
to
local file = fs.open("users/"..usrName,"r")
You were using the wrong mode :P/>
But you will also need to use the loop I told you or else that will error too
695 posts
Location
In my basement.
Posted 30 March 2013 - 06:53 AM
Well… I( get the error: test:640: attempt to call nil. line 640 is in this case line 31
Code:
function changepass()
term.clear()
term.setCursorPos(1,1)
write("Username: ")
local usrname = read()
write("Old password: ")
local oldpass = read("*")
write("New password: ")
local newpass = read("*")
if not fs.exists("users/"..usrname) then
term.clear()
term.setCursorPos(1,1)
print("No such username!")
sleep(1)
term.clear()
acc()
elseif fs.exists("users/"..usrname) then
local fileData = {}
repeat
local file = fs.open("users/"..usrName,"r")
local line = file.readLine()
while line do
table.insert(fileData, line)
line = file.readLine()
end
until line == nil -- readLine()
file.close()
local oldpassFromFile = fileData[1]
if oldpass == oldpassFromFile then
file.clear()
file.writeLine(newpass)
file.close()
term.clear()
term.setCursorPos(1,1)
print("Changed!")
sleep(1)
term.clear()
acc()
else
term.clear()
term.setCursorPos(1,1)
print("Failure!")
sleep(1)
term.clear()
acc()
end
end
end
231 posts
Posted 30 March 2013 - 07:55 AM
You're calling file.clear(), which a method in any of the modes ('r', 'w', and 'a'). You're also attempting to access the after closing the file (which is a different error).
695 posts
Location
In my basement.
Posted 30 March 2013 - 08:26 AM
help with correcting the code please.
231 posts
Posted 30 March 2013 - 08:57 AM
If you want to clear a file, you can open it in mode 'w'. That will fix both of the problems at once.
local file = fs.open("users/"..usrName,"r")
local line = file.readLine()
while line do
table.insert(fileData, line)
line = file.readLine()
end
until line == nil -- readLine()
file.close()
local oldpassFromFile = fileData[1]
if oldpass == oldpassFromFile then
file = fs.open('users/'..usrName,'w') -- Changed from file.clear()
file.writeLine(newpass)
file.close()
695 posts
Location
In my basement.
Posted 30 March 2013 - 09:21 AM
THANK YOU!! IT WORKED! I spent like one and an half hour on that…