Posted 23 April 2014 - 02:55 AM
So first off I've been writing a "server" script so when it gets a certain message it'll write to a file located on the server, and then read that file for client names, and client balance. My problem is I can write to the file, but it keeps overwriting my previous work, so then i tried table.insert, which in turn made my table nil! Please someone just help me with this one, usually I can find the bugs in my own script, but this one I've been fighting with for too long. here's the script:
modem = peripheral.wrap("right")
rednet.open("right")
originalnames = {}
function save (table, clients) -- a save function
local file = fs.open("config/clients", "w")
file.write(textutils.serialize(originalnames))
file.close()
end
if fs.exists("config") ~= true -- create configure folder for clients and credit
then fs.makeDir("config")
print("Config directory created")
sleep(1)
end
if fs.exists("config/clients") ~= true -- writes {} to config/clients (happens only on first run)
then save(table, originalnames)
print("client file created")
sleep(1)
term.clear()
term.setCursorPos(1,1)
end
function save(table, names) -- a save function
local file = fs.open("config/clients", "w")
file.write(textutils.serialize(table))
file.close()
end
function load(table, names) -- loads clients table into environment
local file = fs.open("config/clients","r")
local data = file.readAll()
file.close()
return textutils.serialize(data)
end
load(table,names) -- actually loads the table
write("input mode: ")
mode = read()
if mode == "new client"
then
write("account number: ")
accn = read("*")
if accn == "1"
then write("name: ")
txt = read()
table.insert(table, 1, txt)
save(names)
elseif accn == "2"
then write("name: ")
txt = read()
table.insert(table, 2, txt)
save(names)
(the rest of the script is from 3-16, plus a whole different segment, this portion is what I've been testing with, and where the error originates)