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

i need help with error--attempt to call table--

Started by roosterhat, 19 September 2012 - 02:30 AM
roosterhat #1
Posted 19 September 2012 - 04:30 AM
Ive made program that is a lock for a door but stores the user info in fs. my program runs fine, but when ever i try to add a new user a second time it tells me that a write statement is trying to call a table
BigSHinyToys #2
Posted 19 September 2012 - 04:44 AM
shore I can fix that just let me telepathically copy your code … no wait I'm not psychic please post your code.
Cranium #3
Posted 19 September 2012 - 04:53 PM
I agree with BigShinyToys. We need to see your code to find the error. We can't help without some knowledge of what you have.
roosterhat #4
Posted 20 September 2012 - 12:20 AM
do you want to see my full code or the part that i think is creating the error?
roosterhat #5
Posted 20 September 2012 - 12:24 AM
heres my full code
Spoiler
selection = 1
choice = "nothing"
word = "nothing"
menu = true
function title()
  term.clear()
  term.setCursorPos(1,1)
  print("Door Lock")
  local x, y = term.getSize()
  local header="-"
  for index = 0, x-2, 1 do
   header = header.."-"
  end
  print(header)
  print(" ")
  print(" ")
end

function start()
  title()
  print("Welcome")
  sleep(2)
  title()
  if (fs.exists("userinfo")==false) then
   fs.makeDir("userinfo")
  end
  if (fs.exists("userinfo/firsttime")==false) then
   open = fs.open("userinfo/firsttime" , "w")
   open.write("first time")
   open.close()
   print("Welcome")
   title()
   print("System Setup")
   print(" ")
   print(" ")
   print("Please enter your name")
   write(":> ")
   local username = io.read()
   print("Please create your password")
   write(":> ")
   local pass = read("*")
   local write = fs.open("userinfo/"..username , "w")
   write.writeLine(pass)
   write.writeLine("Admin")
   write.close()
   title()
   print("Hello "..username.." you are currently this computer's Admin")
   print("To finish setup close this program, copy it to this terminal, and rename is as startup")
   print(":Hit Any Key to Continue")
   Event, key = os.pullEvent("key")
   guimenu()
  end
  if (fs.exists("userinfo/firsttime")==true) then
   promptuserinfo()
  end
end
function promptuserinfo()
  done = false
  while done==false do
   title()
   print("Welcome")
   print(" ")
   print(" ")
   print("Enter Your Username")
   write(":> ")
   local username = io.read()
   print("Enter Your Password")
   write(":> ")
   local password = read("*")
   if (fs.exists("userinfo/"..username)==true) then
    local read = fs.open("userinfo/"..username , "r")
    local pass = read.readLine()
    local type = read.readLine()
    read.close()
    if (pass==password) then
	 if (type=="Employee")then
	  title()
	  print("Acsess Granted")
	  sleep(2)
	  title()
	  print("Opening Door...")
	  sleep(1)
	  rs.setOutput("back" , true)
	  title()
	  print("Door Open")
	  sleep(5)
	  rs.setOutput("back" , false)
	  title()
	  print("Closing Door...")
	  sleep(1)
	 elseif (type=="Admin") then
	  guimenu()
	  done = true
	 end
    else
	 print("//ERROR//??Password is incorrect??")
	 sleep(2)
    end
   else
    print("//ERROR//??Username does not exists??")
    sleep(2)
   end
  end
end
function gui()
title()
  if (selection == 1) then
   print("[Add New Account  ]")
   --word = "[Add New Account  ]"
   --middle(word)
  else
   print(" Add New Account")
   --word = " Add New Account"
   --middle(word)
  end
  if (selection == 2) then
   print("[Delete Account   ]")
   --word = "[Delete Account   ]"
   --middle(word)
  else
   print(" Delete Account")
   --word = " Delete Account"
   --middle(word)
  end
  if (selection == 3) then
   print("[List All Accounts]")
   --word = "[List All Accounts]"
   --middle(word)
  else
   print(" List All Accounts")
   --word = " List All Accounts"
   --middle(word)
  end
  if (selection == 4) then
   print("[LogOut		   ]")
   --word = "[LogOut		   ]"
   --middle(word)
  else
   print(" LogOut")
   --word = " LogOut"
   --middle(word)
  end
  if (selection == 5) then
   print("[Quit Program	 ]")
   --word = "[Quit Program	 ]"
   --middle(word)
  else
   print(" Quit Program")
   --word = " Quit Program"
   --middle(word)
  end
end

function guimenu()
  local done = false
  gui()
  while done==false do
   local Event, key = os.pullEvent("key")
   if (key==208) then
    selection=selection+1
   elseif (key==200) then
    selection=selection-1
   end
   if(selection==0) then
    selection=5
   elseif(selection==6) then
    selection=1
   end
   gui(selection)
   if(key==28) then
    if (selection==1) then
	 choice="add"
	 done=true
    elseif (selection==2) then
	 choice="delete"
	 done=true
    elseif (selection==3) then
	 choice="list"
	 done=true
    elseif (selection==4) then
	 done=true
	 title()
	 print("Logging out")
	 sleep(1)
	 selection = 1
	 promptuserinfo()
    elseif (selection==5) then
	 done = true
	 menu = false
    end
   end
  end
end

function add()
  local type = "nothing"
  local username = "nothing"
  local menu = true
  done = false
  title()
  print("Enter new username")
  while menu==true do
   menu = false
   write(":> ")
   x, y = term.getCursorPos()
   username = io.read()
   local filename = fs.list("userinfo")
   for _,file in ipairs( filename ) do
    if (username==file) then
	 print("//!!ERROR!!Username Already Exists//")
	 sleep(1)
	 term.setCursorPos(1,y)
	 term.clearLine()
	 menu = true
	 break
    end
   end
  end
  print("Enter new password")
  write(":> ")
  local password = read("*")
  while done==false do
   print("Enter status type (Employee/Admin)")
   write(":> ")
   x, y = term.getCursorPos()
   type = io.read()
   if (type=="employee"or"Employee") then
    done = true
   elseif (type=="admin"or"Admin") then
    done = true
   else
    print("Unknown Type")
    sleep(1)
    term.setCursorPos(1,y)
    term.clearLine()
    write(":> ")
   end
  end
  write = fs.open("userinfo/"..username , "w")
  write.writeLine(password)
  if (type=="employee") then
   type="Employee"
  elseif (type=="admin") then
   type="Admin"
  end
  sleep(1)
  write.writeLine(type)
  write.close()
  title()
  print("User Added")
  sleep(1)
  guimenu()
end

function delete()
  title()
  print("Enter the username you would like to delete")
  write(":> ")
  local username = io.read()
  if (fs.exists("userinfo/"..username)==true) then
   fs.delete("userinfo/"..username)
   title()
   print("User deleted")
   sleep(1)
  else
   print("//!!ERROR!!User Does Not Exists//")
   sleep(1)
  end
  guimenu()
end

function list()
  local type = "unknown"
  local count=0
  title()
  local filename = fs.list("userinfo")
  print("Format <username: password: usertype>")
  print(" ")
  for _,file in ipairs( filename ) do
   if (file=="firsttime") then
   else
    local read = fs.open("userinfo/"..file , "r")
    local pass = read.readLine()
    local type = read.readLine()
    print("<"..file..": "..pass..": "..type..">")
    read.close()
    count=count+1
   end
  end
  if (count==0) then
   print("There are no users in this database")
  end
  print(" ")
  print(":Hit Any Key to Continue")
  Event, key = os.pullEvent("key")
  guimenu()
end
--PROGRAM STARTS HERE
start()
while menu==true do
if (choice=="add") then
  add()
elseif (choice=="delete") then
  delete()
elseif (choice=="list") then
  list()
end
end
term.clear()
term.setCursorPos(1,1)
roosterhat #6
Posted 20 September 2012 - 12:26 AM
i think that part that is causing the bug is the function "add"
roosterhat #7
Posted 20 September 2012 - 12:30 AM
the error usually comes up at line 228 //write(":> ")//
Fatal_Exception #8
Posted 20 September 2012 - 11:27 AM
You're doing this:


  write = fs.open("userinfo/"..username , "w")
  write.writeLine(password)

which is overriding write() with your file handle, so when you later do:


  write(":> ")

… you're accessing the table write (your file handle), and trying to call it as a function.
roosterhat #9
Posted 20 September 2012 - 10:30 PM
oh ok thank you