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

Error in Code Help

Started by haydenf96, 30 October 2012 - 01:12 AM
haydenf96 #1
Posted 30 October 2012 - 02:12 AM
122 elseif cmd == "add friend" then
123 print(usr2)
124 nFriend = usr2.."!SP!"
125 nFriend2 = usr.."!SP!"
126 print(nFriend)
127 gol = fs.open("RedBook/"..usr.."/friendlist", "a")
128 gol.write(nFriend)
129 gol.close()
130 gold = fs.open("RedBook/"..usr2.."/friendlist", "a")
131 gold.write(nFriend2)
132 gold.close()
133 runApp()

the error says " Server:131: attempt to index ? (a nil value)
Lyqyd #2
Posted 30 October 2012 - 02:28 AM
That would indicate that it was unable to open the file you specified.
haydenf96 #3
Posted 30 October 2012 - 03:18 AM
but how would i fix it?
Lyqyd #4
Posted 30 October 2012 - 03:35 AM
Make sure the file exists. Check this by having the program print the path it was attempting to open, then checking that the file is there manually.
haydenf96 #5
Posted 30 October 2012 - 03:50 AM
sorry im decently new to lua itself in computercraft how would i do that..
Lyqyd #6
Posted 30 October 2012 - 04:06 AM
Just before line 131, add this line:


print("RedBook/"..usr2.."/friendlist")

Then check to see that the folder and file both exist.
Luanub #7
Posted 30 October 2012 - 04:30 AM
Or you can do something like this:

if not fs.exists("/RedBook"..usr2.."/friendlist") then error("File does not exist") end
haydenf96 #8
Posted 30 October 2012 - 12:50 PM
Im getting errors on your codes
ChunLing #9
Posted 30 October 2012 - 06:59 PM
if fs.exists("/RedBook"..usr2.."/friendlist") then 
-- do what you will with the file
else print("File does not exist") end
haydenf96 #10
Posted 30 October 2012 - 10:40 PM
if fs.exists("/RedBook"..usr2.."/friendlist") then
-- do what you will with the file
else print("File does not exist") end

bios:206: [string "server"]:130: '=' expected
remiX #11
Posted 30 October 2012 - 11:33 PM
Does usr2 have an '/' as the first charater? If not change it to

if fs.exists("/RedBook/"..usr2.."/friendlist") then
haydenf96 #12
Posted 31 October 2012 - 12:56 AM
same error…
Lyqyd #13
Posted 31 October 2012 - 12:58 AM
Post the full code you're using and the full error message.
haydenf96 #14
Posted 31 October 2012 - 04:02 AM

function OpenAll()
	    for _,side in ipairs(rs.getSides()) do
			    rednet.open(side)
	    end
end
function split(str, pat)
local t = { }
local fpat = "(.-)"..pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
  while s do
   if s ~= 1 or cap ~= "" then
    table.insert(t,cap)
   end
   last_end = e+1
   s, e, cap = str:find(fpat, last_end)
  end
  if last_end <= #str then
   cap = str:sub(last_end)
   table.insert(t, cap)
end
return t
end
function runApp()
OpenAll()
id, msg, d = rednet.receive()
if msg ~= nil then
  t = split(msg, "!SP!")
  usr = t[1]
  cmd = t[2]
  if cmd == nil then
   runApp()
  end
  print("--------------------------------------------------")
  print("USER: "..usr)
  print("COMMAND: "..cmd)
  if cmd == "search" then
   usr2 = t[3]
   print("USER 2: "..usr2)
   print("--------------------------------------------------")
  elseif cmd == "status update" then
   status = t[3]
   print("STATUS: "..status)
   print("--------------------------------------------------")
  elseif cmd == "makeUser" then
   password = t[3]
   print("PASSWORD: "..password)
   print("--------------------------------------------------")
  elseif cmd == "logIn" then
   password = t[3]
   print("PASSWORD: "..password)
   print("--------------------------------------------------")
  elseif cmd == "sndfrq" then
   usr2 = t[3]
   print("USER 2: "..usr2)
   print("--------------------------------------------------")
  elseif cmd == "add friend" then
   usr2 = t[3]
   print("USER 2: "..usr2)
   print("--------------------------------------------------")
  elseif cmd == "list" then
   print("--------------------------------------------------")
  elseif cmd == "deleteUser" then
   usr2 = t[3]
   print("--------------------------------------------------")
  elseif cmd == "listUsers" then
   print("--------------------------------------------------")
  end
  if cmd == "search" then
   if fs.isDir("RedBook/"..usr2) == false then
    rednet.send(id, "USER DOES NOT EXIST")
   else
    if fs.exists("RedBook/"..usr2.."/status") == true then
	 usrstatus1 = fs.open("RedBook/"..usr2.."/status", "r")
	 usrstatus = usrstatus1.readAll()
	 usrstatus1.close()
    end
    x = usr2.."!SP!"..usrstatus.."!SP!"
    rednet.send(id, x)
   end
   runApp()
  elseif cmd == "status update" then
   f = fs.open("RedBook/"..usr.."/status", "w")
   f.write(status)
   f.close()
   runApp()
  elseif cmd == "list" then
   h = fs.open("RedBook/"..usr.."/friendlist", "a")
   h.close()
   friends = fs.open("RedBook/"..usr.."/friendlist", "r")
   xyz = friends.readAll()
   print(xyz)
   rednet.send(id, xyz)
   runApp()
  elseif cmd == "sndfrq" then
   check = fs.open("RedBook/"..usr2.."/friendlist", "r")
   chck2 = check.readAll()
   chck3 = split(chck2, "!SP!")
   check.close()
   a = fs.open("RedBook/"..usr2.."/friendreq", "a")
   a.write("RedBook/"..usr.."!SP!")
   a.close()
   runApp()
  elseif cmd == "chck frnd req" then
   if fs.exists("RedBook/"..usr.."/friendreq") == false then
    go = fs.open(usr.."/friendreq", "a")
    go.close()
   end
   chck = fs.open("RedBook/"..usr.."/friendreq", "r")
   cv = chck.readAll()
   chck.close()
   clear = fs.open("RedBook/"..usr.."/friendreq", "w")
   clear.close()
   print(cv)
   rednet.send(id, cv)
   print("SENT")
   print("--------------------------------------------------")
   chck.close()
   runApp()
  elseif cmd == "add friend" then
   print(usr2)
   nFriend = usr2.."!SP!"
   nFriend2 = usr.."!SP!"
   print(nFriend)
   gol = fs.open("RedBook/"..usr.."/friendlist", "a")
   gol.write(nFriend)
   gol.close
   gold = fs.open("RedBook/"..usr2.."/friendlist", "a")
   if fs.exists("RedBook/"..usr2.."/friendlist") then
   gold.write(nFriend2)
   else print("File does not exist") end
   gold.close()
   runApp()
  elseif cmd == "logIn" then
   if fs.isDir("RedBook/"..usr) == true then
    w = fs.open("RedBook/"..usr.."/password", "r")
    y = w.readAll()
    if y == password then
	 rednet.send(id, "YOU HAVE BEEN LOGGED IN")
    else
	 rednet.send(id, "ERROR: WRONG PASSWORD")
    end
   else
    rednet.send(id, "ERROR: WRONG USERNAME")
   end
   runApp()
  elseif cmd == "makeUser" then
   fs.makeDir("RedBook/"..usr)
   make1 = fs.open("RedBook/"..usr.."/friendreq", "a")
   make1.close()
   make2 = fs.open("RedBook/"..usr.."/status", "a")
   make2.close()
   make3 = fs.open("RedBook/"..usr.."/friendlist", "a")
   make3.close()
   make4 = fs.open("RedBook/"..usr.."/password", "w")
   make4.write(password)
   make4.close()
   rednet.send(id, "USER HAS BEEN CREATED")
   runApp()
  elseif cmd == "listUsers" then
   list = fs.list("RedBook/")
   list2 = textutils.serialize(list)
   rednet.send(id, list2)
   runApp()
  elseif cmd == "deleteUser" then
   if fs.exists("RedBook/"..usr2) then
    fs.delete("RedBook/"..usr2)
    rednet.send(id, "USER IS GONE >:^D")
   else
    rednet.send(id, "USER DOES NOT EXIST")
   end
   runApp()
  end
end
end
runApp()

The error says: bios:206: [string "server"]:130: '=' expected
Lyqyd #15
Posted 31 October 2012 - 04:49 AM
When I copy and paste it out, it's line 127, but gol.close needs the parentheses. It should be gol.close().
ChunLing #16
Posted 31 October 2012 - 07:52 AM
You are patient as water.