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

[Lua] [Error] Error with passcode system

Started by MemoryLeak21, 06 December 2012 - 05:02 PM
MemoryLeak21 #1
Posted 06 December 2012 - 06:02 PM
Well, this is embarassing. Here's my program:


--[[ Load API ]]--
os.loadAPI("redos")
--[[ Local Variables ]]--
local username
local password
local eUsername
local ePassword
local line
local file
local running = true
--[[ Main ]]--
while running do
  redos.clear()
  redos.header()
  file = fs.open("systemdata/first.txt", "r")
  line = file.readLine()
  file.close()

  if line == "true" then
	write("Register username: ")
	username = read()

	redos.clear()
	redos.header()

	write("Register password: ")
	password = read("*")

	if not fs.exists("userdata/"..username) then
	  fs.makeDir("userdata/"..username)
	  file = fs.open("userdata/"..username.."/password.txt", "w")
	  file.writeLine(password)
	  file.close()
	end

	file = fs.open("systemdata/first.txt", "w")
	file.writeLine("false")
	file.close()

	running = false

	redos.clear()
	redos.header()
  end

  write("Enter username: ")
  eUsername = read()

  redos.clear()
  redos.header()

  write("Enter password: ")
  ePassword = read("*")

  redos.clear()
  redos.header()

  if fs.exists("userdata/"..eUsername) then
	file = fs.open("userdata/"..eUsername.."/password.txt")
	line = fs.readLine()
	file.close()
	if line == ePassword then
	  print("Welcome,"..eUsername.."!")
	  os.sleep(1)
	  running = false
	end
  end
end

The code works fine until I enter the password for the system, and it gives me:

pass: 65: bad argument: string expected, got nil

I've run through all debugging methods I know of, and got zilch. Here's my system hierarchy:

systemdata > first.txt

userdata > MemoryLeak21 > password.txt

And my API just has header() and clear(), which do visual and unimportant things.

Anyone recognize the problem?
ChunLing #2
Posted 06 December 2012 - 08:05 PM
I'm not able to match line 65 with anything that would cause that. Can you use the edit program and post line 65 as it appears in the copy in CC?
KaoS #3
Posted 07 December 2012 - 12:07 AM
The only explanation I see is if 'eUsername' is nil however there is no situation in which that should occur, even if you do not enter anything for read it returns a blank string ("") perhaps your API is fiddling with your vars
remiX #4
Posted 07 December 2012 - 07:01 AM
Please specify within your code which line is line 65 so we know exactly and don't have to search through it. I copied it now into Notepad++ and my line 65 is 'running = false'
MemoryLeak21 #5
Posted 07 December 2012 - 11:11 AM
I'm absolutely sure that the API isn't doing anything, as all it does is display the header or clear the screen. Here's my line 65:

        file = fs.open("userdata/"..eUsername.."/password.txt")
Lyqyd #6
Posted 07 December 2012 - 11:17 AM
Try specifying the mode to open the file with.
MemoryLeak21 #7
Posted 07 December 2012 - 11:24 AM
Oh wow, I'm dumb as always. Thank you so much!
KaoS #8
Posted 07 December 2012 - 11:32 AM
lol, I feel just as stupid. blind lol
ChunLing #9
Posted 07 December 2012 - 12:06 PM
Don't feel bad, it's hard to spot an error when you're looking in the wrong place. I only checked about three lines up and down before giving up and asking for the exact line.
ChaddJackson12 #10
Posted 07 December 2012 - 03:08 PM
Just a question, can you call empty variables? That might be your problem.
MemoryLeak21 #11
Posted 08 December 2012 - 08:34 AM
Nah, I've called empty variables before in other programs and it's worked fine.
ChaddJackson12 #12
Posted 08 December 2012 - 12:18 PM
Nah, I've called empty variables before in other programs and it's worked fine.
What's the point of calling an empty variable, though? That is, if they aren't a boolean value, or whatever those are…
MemoryLeak21 #13
Posted 08 December 2012 - 02:01 PM
Nah, I've called empty variables before in other programs and it's worked fine.
What's the point of calling an empty variable, though? That is, if they aren't a boolean value, or whatever those are…

Well, I get used to organizing it that way so I have a quick reference of all the variables I'll be using in my program. It also avoids the creation of global variables, which are always messy.
ChunLing #14
Posted 08 December 2012 - 10:48 PM
The terminology there is "declare", not call. Declaring a variable without an assignment is perfectly valid (though I usually put them all on one line and assign nil, like: local username,password,eUsername,ePassword,line,file = nil just for clarity –though it's important that only the first one gets assigned that way). Calling a variable would usually mean either calling it as a function or passing it to a function as an argument. You usually want it to no longer be nil before doing either of those.