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

Table Issue.

Started by mrdawgza, 25 September 2013 - 11:36 AM
mrdawgza #1
Posted 25 September 2013 - 01:36 PM
So I have an operating system, and I'm making a account management system.
So I have a login program, which had the username and passwords in that program.
Now I have the usernames and passwords in a separate program from the login.
I have done what I think is right with the tables.

Here is the code for the login system:
Spoiler

--Login system

--username = {"admin", "account2", "account3"}  --I've turned this off,
--password = {"pass", "account2", "account3"} --I've turned this off,

function load(users)
local file = fs.open(users,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

function load(users)
local file = fs.open(pass1,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

term.setCursorPos(18,4)
print("Login:")
term.setCursorPos(18,5)
print("------")
term.setCursorPos(18,6)
write("Username: ")
user = read()
term.setCursorPos(18,7)
write("Password: ")
pass = read("*")

for i=1, #user do 
 if user == users[i] and pass == pass1[i] then
  access = true
 end
end

if access == true then
print("Welcome, "..user.."!")
sleep(1)
term.clear()
term.setCursorPos(1,22)
print("Welcome, "..user.."!")
sleep(1)
term.clear()
term.setCursorPos(1,1)
shell.run("mainmenu")
else
print("Sorry! You entered the password or username incorrectly! Retry,")
sleep(2)
shell.run("login")
end

Here is the code for the 'users' program or file:
(Where I store the usernames and passwords.)
Spoiler


local users = {user1 = "admin", user2 = "2nd"}
local pass1 = {pass1 = "pass", pass2 = "2nd"}

Now when I run 'logon' (what I named the first code) I get this error:


logon:42: attempt to call index ? (a nil value)

Can anyone please help me? I am stuck on this. I've tried almost everything I think would fix it and Googling it doesn't help at all.
LBPHacker #2
Posted 25 September 2013 - 01:45 PM
YAY you used spoilers! Thanks a bunch! Now the problems:
  • You have two load functions (not a problem, but makes no sense)
  • users is never declared, so it's treated as a global
  • You never actually call load
  • The unserialization is messed up
And the solutions:
  • Remove the second load function
  • Rename the load function to loadUsers (load in itself could mean anything)
  • Insert local users before the first line
  • Insert users = loadUsers() after declaring loadUsers
​And about the unserialization - the users file isn't unserializable, because it's an actual program. You have two options:
  • Have some properly serialized table in the users file
That would require you to throw together the users file like this:

{
users = {user1 = "admin", user2 = "2nd"},
pass1 = {pass1 = "pass", pass2 = "2nd"}
}
And you would access that in the program like this:
if user == users.users[i] and pass == users.pass1[i] then
  • Or load users with some function environment magic:
That would require you to get rid of the locals in the users file, and the loadUsers function would look like this:
local function loadUsers()
local env = {}
setfenv(loadfile("users"), env)()
return env
end
Of course, you'd have to access the data the same way you would do with the properly serialized table.
mrdawgza #3
Posted 25 September 2013 - 02:24 PM
YAY you used spoilers! Thanks a bunch! Now the problems:
  • You have two load functions (not a problem, but makes no sense)
  • users is never declared, so it's treated as a global
  • You never actually call load
  • The unserialization is messed up
And the solutions:
  • Remove the second load function
  • Rename the load function to loadUsers (load in itself could mean anything)
  • Insert local users before the first line
  • Insert users = loadUsers() after declaring loadUsers
​And about the unserialization - the users file isn't unserializable, because it's an actual program. You have two options:
  • Have some properly serialized table in the users file
That would require you to throw together the users file like this:

{
users = {user1 = "admin", user2 = "2nd"},
pass1 = {pass1 = "pass", pass2 = "2nd"}
}
And you would access that in the program like this:
if user == users.users[i] and pass == users.pass1[i] then
  • Or load users with some function environment magic:
That would require you to get rid of the locals in the users file, and the loadUsers function would look like this:
local function loadUsers()
local env = {}
setfenv(loadfile("users"), env)()
return env
end
Of course, you'd have to access the data the same way you would do with the properly serialized table.

Ok, I used spoilers, is that a good thing?
And, I also tried to fix it.

I tried both ways for the serialiazation (think its called that), but a new error occurs.

New logon code:
Spoiler

term.clear()
term.setCursorPos(1,1)
--Login system

--username = {"admin", "account2", "account3"}  --I've turned this off,
--password = {"pass", "account2", "account3"} --I've turned this off,

local function loadUsers()
local env = {}
setfenv(loadfile("users"), env)()
return env
end



term.setCursorPos(18,4)
print("Login:")
term.setCursorPos(18,5)
print("------")
term.setCursorPos(18,6)
write("Username: ")
user = read()
term.setCursorPos(18,7)
write("Password: ")
pass = read("*")

for i=1, #user do
if user == users.users[i] and pass == users.pass1[i] then
  access = true
end
end

if access == true then
print("Welcome, "..user.."!")
sleep(1)
term.clear()
term.setCursorPos(1,22)
print("Welcome, "..user.."!")
sleep(1)
term.clear()
term.setCursorPos(1,1)
shell.run("mainmenu")
else
print("Sorry! You entered the password or username incorrectly! Retry,")
sleep(2)
shell.run("login")
end

New users code:
Spoiler


{
users = {user1 = "admin", user2 = "2nd"}
pass1 = {pass1 = "pass", pass2 = "2nd"}
}

The error is still

logon:26: attemp to index ? (a nil value)

I think I've done something wrong, if I have what is it? I did the solutions in the way I read them so…
LBPHacker #4
Posted 25 September 2013 - 02:27 PM
  • On line 25 (or wherever the for i=1, #user do part is), get the length of users.users, not user.
  • You forgot a comma at the end of the second line in the users file.
EDIT: See, users.users, not users.
mrdawgza #5
Posted 25 September 2013 - 02:31 PM
I can't get this right.

Now it's giving error:


logon:27: attempt to get lengh of nil

with


for i=1, #users do 
 if user == users.users[i] and pass == users.pass1[i] then
  access = true
 end
end

and



{
users = {user1 = "admin", user2 = "2nd"},
pass1 = {pass1 = "pass", pass2 = "2nd"}
}
LBPHacker #6
Posted 25 September 2013 - 02:33 PM
  • Remove the second load function
  • Rename the load function to loadUsers (load in itself could mean anything)
  • Insert local users before the first line
  • Insert users = loadUsers() after declaring loadUsers
mrdawgza #7
Posted 25 September 2013 - 02:41 PM
I did exactly that again;

logon file.

local users
local function loadUsers()
local env = {}
setfenv(loadfile("users"), env)()
return env
end
users = loadUsers()

users file.

{
users = {user1 = "admin", user2 = "2nd"},
pass1 = {pass1 = "pass", pass2 = "2nd"}
}

Error:

logon:11: attempt to call nil

Either my brain isn't listening today, or I dunno.
Dunno.
Sorry if I'm irritating you or being a pain.
LBPHacker #8
Posted 25 September 2013 - 02:50 PM
You should have left the parts of the program untouched which I didn't mention… But seriously, how did you manage to have an error on line 11 in a 7-line long program?!

Sorry if I'm irritating you or being a pain.
Nah, don't worry. We (the helping ones) are here because we want to help.
mrdawgza #9
Posted 25 September 2013 - 02:54 PM
You should have left the parts of the program untouched which I didn't mention… But seriously, how did you manage to have an error on line 11 in a 7-line long program?!

Sorry if I'm irritating you or being a pain.
Nah, don't worry. We (the helping ones) are here because we want to help.
Hehe, the program isn't 7 lines long. I just cut off a shortened piece to put here. Heres the full one.


term.clear()
term.setCursorPos(1,1)
--Login system

--username = {"admin", "account2", "account3"}  --I've turned this off,
--password = {"pass", "account2", "account3"} --I've turned this off,

local users
local function loadUsers()
local env = {}
setfenv(loadfile("users"), env)()
return env
end
users = loadUsers()


term.setCursorPos(18,4)
print("Login:")
term.setCursorPos(18,5)
print("------")
term.setCursorPos(18,6)
write("Username: ")
user = read()
term.setCursorPos(18,7)
write("Password: ")
pass = read("*")

for i=1, #users do 
 if user == users.users[i] and pass == users.pass1[i] then
  access = true
 end
end

if access == true then
print("Welcome, "..user.."!")
sleep(1)
term.clear()
term.setCursorPos(1,22)
print("Welcome, "..user.."!")
sleep(1)
term.clear()
term.setCursorPos(1,1)
shell.run("mainmenu")
else
print("Sorry! You entered the password or username incorrectly! Retry,")
sleep(2)
shell.run("login")
end

What did I touch that should be untouched?
theoriginalbit #10
Posted 25 September 2013 - 02:58 PM
I really do suggest that you stop at this point, learn about functions and loops and move everything out of different programs and just have it all in one program. It will make your life MUCH easier, and doing it now will be better than doing it later.

There is also much better ways of creating and storing accounts, take a look at these two links for example
Read All
Read the Reply
I would post some things others have said on the matter, but I can't remember any other good examples, and search wasn't turning up any good ones.
LBPHacker #11
Posted 25 September 2013 - 02:59 PM
Well, you left untouched everything except for the users file, which should look like this:
users = {user1 = "admin", user2 = "2nd"}
pass1 = {pass1 = "pass", pass2 = "2nd"}
As I mentioned, get rid of the locals. You chose the second option, but that doesn't require you to make a table of the contents of users.
mrdawgza #12
Posted 25 September 2013 - 03:02 PM
Ok, now no errors, but it immediately says You entered the wrong password or username when I actually put in one of the correct username or password.
LBPHacker #13
Posted 25 September 2013 - 03:08 PM
Ohh… well, my bad - users should look like this:
users = {"admin", "2nd"}
pass1 = {"pass", "2nd"}
mrdawgza #14
Posted 25 September 2013 - 03:14 PM
It should work now. I'll test it just now, I'm playing GTA V at the moment. Erm,
also another question, as I stated first, I am planning to change a username or password from within my operating system. How can I do such?
theoriginalbit #15
Posted 25 September 2013 - 03:16 PM
also another question, as I stated first, I am planning to change a username or password from within my operating system. How can I do such?

I really do suggest that you stop at this point, learn about functions and loops and move everything out of different programs and just have it all in one program. It will make your life MUCH easier, and doing it now will be better than doing it later.

There is also much better ways of creating and storing accounts, take a look at these two links for example
Read All
Read the Reply
I would post some things others have said on the matter, but I can't remember any other good examples, and search wasn't turning up any good ones.
mrdawgza #16
Posted 25 September 2013 - 03:18 PM
also another question, as I stated first, I am planning to change a username or password from within my operating system. How can I do such?

I really do suggest that you stop at this point, learn about functions and loops and move everything out of different programs and just have it all in one program. It will make your life MUCH easier, and doing it now will be better than doing it later.

There is also much better ways of creating and storing accounts, take a look at these two links for example
Read All
Read the Reply
I would post some things others have said on the matter, but I can't remember any other good examples, and search wasn't turning up any good ones.
Woops, sorry I never saw your first post :P/>
Ill look into them now…
mrdawgza #17
Posted 25 September 2013 - 03:26 PM
It doesn't work. I just tested it. It still just says I entered wrong user details.
LBPHacker #18
Posted 25 September 2013 - 03:31 PM
Take a look at your PMs…