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

Problem with Password System (attempt to index ? (a nil value))

Started by MemoryLeak21, 29 March 2013 - 04:48 PM
MemoryLeak21 #1
Posted 29 March 2013 - 05:48 PM
I've been working on a computer system with a login screen (back into ComputerCraft, yay!) When I run the program, everything works just fine, until I input the username; the error is:

startup:51: attempt to index ? (a nil value)

Line 51 is:

	  local passOut = file.readAll()

And the whole code is:
Spoiler

rednet.open("right")
print("					  YellowOS")
print("")
local first = true
local correct = false
if not fs.exists("bin/exists") then
  local same = false

  fs.makeDir("bin")
  local f = fs.open("bin/exists", "w")
  f.write("true")
  f.close()
  fs.makeDir("bin/users")

  while not same do
	term.clear()
	term.setCursorPos(1, 1)
  
	if not first then
	  print("				  YellowOS")
	  print("")
	end
  
	print("Create new user: ")
	local userIn = read()
	print("New password: ")
	local passIn = read("*")
	print("Confirm password: ")
	local confirmIn = read("*")
  
	if passIn == confirmIn then
	  fs.makeDir("bin/users/"..userIn)
	  f = fs.open("bin/users/"..userIn.."/pass", "w")
	  f.write(passIn)
	  f.close()
	  same = true
	end
  
	first = false
  end
else
  while not correct do
	print("Enter user: ")
	local user = read()
	if fs.exists("bin/users/"..user)then
	  local file = fs.open("bin/user/"..user.."/pass", "r")
	  local passOut = file.readAll()
	  print("Enter password: ")
	  local pass = read("*")
	else
	  print("Incorrect username!")
	end
  
	if not pass == passOut then
	  print("")
	  print("Incorrect password!")
	  os.sleep(2000)
	else
	  correct = true
	end
  end
end

Can anybody recognize the problem?
faubiguy #2
Posted 29 March 2013 - 08:01 PM
It looks like the '/user/' in
local file = fs.open("bin/user/"..user.."/pass", "r")
needs to be corrected to '/users/', and to add a file.close() after the file.readAll(). After fixing both of those, the code appears to work.
Mads #3
Posted 29 March 2013 - 11:32 PM
Why is the users/ dir in the bin folder? :D/>
MemoryLeak21 #4
Posted 30 March 2013 - 04:26 AM
It looks like the '/user/' in
local file = fs.open("bin/user/"..user.."/pass", "r")
needs to be corrected to '/users/', and to add a file.close() after the file.readAll(). After fixing both of those, the code appears to work.

As always, it is extremely obvious. I should really learn to pay more attention to my code… Thanks!