If you want to go the lazy route:
--# make our table of usernames and passwords
local users = {
user1 = "pass";
user2 = "pass";
}
--# save them to a file with textutils.serialize()
local file = fs.open('users', 'w')
file.write(textutils.serialize(users))
file.close()
--# load them using textutils.unserialize()
local file = fs.open('users', 'r')
local content = textutils.unserialize(file.readAll())
file.close()
--# make sure the user hasn't tampered with the file
--# if they have, the format will be screwed up, and content will be null
--# so we simply check if content exists
if content then
users = content
end
textutils.serialize() is a function used to convert tables into strings, that are able to be loaded later using textutils.unserialize(). It stores them in a format like this:
{["user1"]="pass",["user2"]="pass",}
Which is only somewhat human-readable.
If you want to be
really human-readable, that'll take a bit of effort with string matching and such.
--# using our "users" table defined in the previous example
--# keep a table of lines for use later
local lines = {}
--# go through all of the users, and add their username and password as a new line
for username, password in pairs(user) do
--# \t is a tab character
--# this looks pretty in text editors and that's pretty much it
local line = username..'\t'..password
--# add to the lines table
table.insert(lines, line)
end
--# now we write all of the lines to our file.
local file = fs.open('users', 'w')
for i=1, #lines do
file.writeLine(lines[i])
end
file.close()
--# for loading, we're going to read our file again, and store its contents in "content"
local file = fs.open('users', 'r')
local content = file.readAll()
file.close()
--# and of course empty our "users" table
users = {}
--# then use string matching to match every line.
--# the difference between gmatch and match, is that gmatch loops through every match found
--# and match only gives you one match.
for line in content:gmatch('[^\n]+') do --# matches "everything but a newline character"
--# then the username and password
local user, pass = line:match('(%S+)%s(%S+)') --# matches two groups of non-space characters sandwiching a space character. e.g. "(John) (Boehner)"
--# after that, we just add the username and password to the table.
users[user] = pass
end
The above example produces and reads a file format like this:
user1 pass
user2 pass
More on strings and string matching (you'll mainly want to look at "string.match", "string.gmatch", and "Patterns"):
http://www.lua.org/manual/5.1/manual.html#5.4