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

[Help Needed]Another Post About Passwords Program / Attempt To Call Nil

Started by Raxe88, 29 July 2013 - 07:32 AM
Raxe88 #1
Posted 29 July 2013 - 09:32 AM
Ok, I'm getting into the world of tables/for loops/fs and I'm having problems (not a lot actually). The first problem that I have to face is that I can't run a single function. This is the entire code:

print("do you want to login or register? R/L")
input0 = read()
if input0 == "R" then
  register()
  --shell.run(register)
elseif input0 == "L" then
  --shell.run(login)
  login()
else
  print("Incorrect answer! :(/>/>/> Try again!")
  sleep(2)
  sell.run(startup)
end
----[[
function login()
r = fs.open("tablesData/file","r")
usersNot = r.readLine()
passwordsNot = r.readLine()
users = textutils.unserialize(usersNot)
passwords = textutils.unserialize(passwordsNot)
r.close()
write("enter the username: ")
input1 = read()
write("enter the password: ")
input2= read()
loged = false
for i = 1, #passwords do
   if users[i] == input and passwords[i] == input2 then
	 loged = true
	end
end
print(loged)
if loged == true then
  print("Welcome")
else
  print("Get out of hier")
  sleep(2)
  os.reboot()
end
end
function register()
write("Please enter your username: ")
input3 = read()
write("Please enter your password: ")
input4 = read("*")
write("Please repeat your password: ")
input5 = read("*")
t = fs.open("tablesData/files","r")
usersGet = t.readLine()
passwordsGet = t.readLine()
t.close()
usersGot = textutils.unserialize(usersGet)
passwordsgot = textutils.unserialize(passwordsGet)
w = fs.open("tablesData/files","w")
if input4 == input5 then
  table.insert(usersGot,#usersGot+1,input3)
  table.insert(passwordsGot,#passwordsGot+1,input5)
  usersC = textutils.serialize(usersGot)
  passwordsC = textutils.serialize(passwordsGot)
  w.writeLine(usersC)
  w.writeLine(passwordsC)
else
  print("The passwords don't mismatch, try again.")
  register()
end
end
--]]--

And this is my file (tablesData/file):

{[1]="admin",[2]="user1",[3]="user2",}
{[1]="admin",[2]="pass1",[3]="pass2",}

At this point, when I run this, I get this error when registering: startup:8: attempt to call nil
And when login: startup:12: attempt to call nil

I don't understand why or what couses it and I can't figure out for myself how to solve it, I searched in the wiki, the forums and nothing that could fix it. I also tried making the function login and register another program and using shell.run() but then when I enter R or L nothing happens….

Thanks for reading.
LBPHacker #2
Posted 29 July 2013 - 11:02 AM
Line 12: sell.run. Should be shell.

AND: Look at this:
shell.run(register)
Lua thinks that register is a variable. Actually, it is, and it is nil. shell.run accepts a string parameter, so if the name of the program is register, you can run it like this:
shell.run("register")
Same with the other shell.runs.

EDIT: You should really use locals.

EDIT #2: Grammar time: The message on two mismatching passwords should be "The passwords don't match".
Raxe88 #3
Posted 29 July 2013 - 11:32 AM
Line 12: sell.run. Should be shell.

AND: Look at this:
shell.run(register)
Lua thinks that register is a variable. Actually, it is, and it is nil. shell.run accepts a string parameter, so if the name of the program is register, you can run it like this:
shell.run("register")
Same with the other shell.runs.

EDIT: You should really use locals.

EDIT #2: Grammar time: The message on two mismatching passwords should be "The passwords don't match".
Thanks for the help. I should improve my grammar. I changed that "mismatch" in my code after I published this but I didn't remember to do it here >.<
That "sell" thing was a fail but I didn't know how it worked.
I'll look forward using locals next time.

Now I'm running into more problems…
Problem 1: register:9: attempt to index ? (a nil value) / Is something wrong with the readLine()? I used it before and worked well…

--register
write("Please enter your username: ")
input3 = read()
write("Please enter your password: ")
input4 = read("*")
write("Please repeat your password: ")
input5 = read("*")
t = fs.open("tablesData/files","r")
usersGet = t.readLine()
passwordsGet = t.readLine()
t.close()
usersGot = textutils.unserialize(usersGet)
passwordsgot = textutils.unserialize(passwordsGet)
if input4 == input5 then
  w = fs.open("tablesData/files","w")
  table.insert(usersGot,#usersGot+1,input3)
  table.insert(passwordsGot,#passwordsGot+1,input5)
  usersC = textutils.serialize(usersGot)
  passwordsC = textutils.serialize(passwordsGot)
  w.writeLine(usersC)
  w.writeLine(passwordsC)
  w.close()
else
  print("The passwords don't match, try again.")
  register()
end

Problem 2: When loging in, I put in the correct user and password but, It doesn't log in. (I'll try to solve it, but help would be appreciated) – I fixed it! :D/> I changed input to input1, derp.

--login
--function login()
r = fs.open("tablesData/file","r")
usersNot = r.readLine()
passwordsNot = r.readLine()
users = textutils.unserialize(usersNot)
passwords = textutils.unserialize(passwordsNot)
r.close()
write("enter the username: ")
input1 = read()
write("enter the password: ")
input2= read()
loged = false
for i = 1, #passwords do
   if users[i] == input and passwords[i] == input2 then
	 loged = true
	end
end
if loged == true then
  print("Welcome")
else
  print("Get out of hier")
  sleep(2)
  os.reboot()
end
--end

Thanks for reading and helping :)/>
LBPHacker #4
Posted 29 July 2013 - 01:45 PM
When you get an attempt to index nil, that doesn't mean that the index itself is wrong, since if the value under a random key isn't set and you index that key, you'll get a nil, not an error. The problem is that the table itself, t is nil (the file handle). fs.open returned nil - that (in read mode) means that the file doesn't exist.
Raxe88 #5
Posted 29 July 2013 - 04:20 PM
When you get an attempt to index nil, that doesn't mean that the index itself is wrong, since if the value under a random key isn't set and you index that key, you'll get a nil, not an error. The problem is that the table itself, t is nil (the file handle). fs.open returned nil - that (in read mode) means that the file doesn't exist.

Again, thanks for helping me, looks like everything should work now! But doesn't! D:

So, I changed this:

t = fs.open("tablesData/files","r")
Into:

t = fs.open("tablesData/file","r")

Now the only error that I get is (when login and registering): textutils:177:attempt to concatenate string and nil! Woho! More nil! Free nil!! I'm so pissed of my typing errors that I'm sure this one will also be something like it.