Jump to content




[Lua] [Error] Error with passcode system


13 replies to this topic

#1 MemoryLeak21

  • Members
  • 30 posts

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?

#2 ChunLing

  • Members
  • 2,027 posts

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?

#3 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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'

#5 MemoryLeak21

  • Members
  • 30 posts

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")


#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 December 2012 - 11:17 AM

Try specifying the mode to open the file with.

#7 MemoryLeak21

  • Members
  • 30 posts

Posted 07 December 2012 - 11:24 AM

Oh wow, I'm dumb as always. Thank you so much!

#8 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 07 December 2012 - 11:32 AM

lol, I feel just as stupid. blind lol

#9 ChunLing

  • Members
  • 2,027 posts

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.

#10 ChaddJackson12

  • Members
  • 264 posts

Posted 07 December 2012 - 03:08 PM

Just a question, can you call empty variables? That might be your problem.

#11 MemoryLeak21

  • Members
  • 30 posts

Posted 08 December 2012 - 08:34 AM

Nah, I've called empty variables before in other programs and it's worked fine.

#12 ChaddJackson12

  • Members
  • 264 posts

Posted 08 December 2012 - 12:18 PM

View PostMemoryLeak21, on 08 December 2012 - 08:34 AM, said:

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...

#13 MemoryLeak21

  • Members
  • 30 posts

Posted 08 December 2012 - 02:01 PM

View PostChaddJackson12, on 08 December 2012 - 12:18 PM, said:

View PostMemoryLeak21, on 08 December 2012 - 08:34 AM, said:

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.

#14 ChunLing

  • Members
  • 2,027 posts

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users