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

Attempt to index ? (a nil value)

Started by Abahu, 23 December 2013 - 04:20 PM
Abahu #1
Posted 23 December 2013 - 05:20 PM
Hi, I'm making a system that monitors turtles and their digging progress. I've implemented a mapping system and a system to take over a turtle at any given time. I've seem to run into a problem when I tried making a system to save and later load my map. Here's the snippet of code:


function save(f)
if fs.exists("mine/map" .. tostring(f)) then fs.delete("mine/map" .. tostring(f)) end
local file = fs.open("mine/map" .. tostring(f), "w")
file.write(textutils.serialize(_G["a" .. tostring(f)]))
file.close()
end
function load(f)
local file = fs.open("mine/map" .. tostring(f), "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

and the program returns the error message stated in the title at the line "file.write(textutils.serialize(_G["a" .. tostring(f)]))"
Why is this? How can I fix this?
awsmazinggenius #2
Posted 23 December 2013 - 06:13 PM
Have you read the sticky post entitled "Read this before asking questions"?

Also, I see that you are new here. Have you read any sticky posts, or the global announcement entitled "Forum Guidelines"?

EDIT: In addition, I would be more likely to take a detailed look at your code if it was indented.

EDIT 2: Also, can you post your whole code? It would be easier to debug. If you don't want to post it, you can PM me it.
Edited on 23 December 2013 - 05:31 PM
Abahu #3
Posted 23 December 2013 - 07:51 PM
Sorry, I didn't read that >.< I should've, sorry. However, I don't know why this happens The code itself is quite long, but here you go:

Spoiler

a11 = {} --Coordinate arrays where the number is the level
a12 = {} -- 0 is solid, 1 is clear, 2 is unknown, any higher is a turtle
a13 = {}
u = {}
ard = {} --Robot's directions, id, last entered id and the controlling robot
aid = {}
lid = 0
cid = 0
cx = 64 --Cursor position
cy = 64
cxp = 64
cyp = 64
lvl = 11
done = true
menu = 0
option = 1
mo = {}
tm = 0
function ia() --Initialise our coordinate arrays
for i=1, 256 do
  a11[i] = {}
  a12[i] = {}
  a13[i] = {}
  for j=1, 256 do
   a11[i][j] = 3
   a12[i][j] = 3
   a13[i][j] = 3
  end
end
save(11)
save(12)
save(13)
for i=1, 8 do
  ard[i] = 0
  aid[i] = 0
end
mo[1] = 5 --main menu
mo[2] = lid + 1 --last
mo[3] = 2 --quit
end
function cu(id, xv, yv) --Sends the value of a coordinate
rednet.send(id, tostring(ax[xv]))
rednet.send(id, tostring(ay[yv]))
end
function du() --updates the display
term.clear()
term.setCusorPos(1,1) --GUI
term.write("==================================================")
term.setCursorPos(1,2)
term.write("|  y=13  y=12  y=13							  |")
term.setCursorPos(1,3)
term.write("==================================================")
if lvl == 11 then
  term.setCursorPos(3,2)
  term.write(">")
end
if lvl == 12 then
  term.setCursorPos(3,2)
  term.write(">")
end
if lvl == 13 then
  term.setCursorPos(3,2)
  term.write(">")
end

for xv = 1, 50 do --Map
  for yv = 4, 18 do
   cm = _G["a"  ..  tostring(lvl)][cx+xv-25][cy+yv-9]
   if cm == 0 then
	term.setCursorPos(xv,yv)
	term.write("#")
   end
   if cm == 1 then
	term.setCursorPos(xv,yv)
	term.write("-")
   end
   if cm == 2 then
	term.setCursorPos(xv,yv)
	term.write(" ")
   end
   for i = 1, lid do
	if cm == aid[i] + 4 then --If a turtle is there, checks if the registered turtle is there, the +4 is to reserve the 0-3 for our datatypes
	 term.setCursorPos(xv,yv)
	 term.write(ard[i])
	 break
	end
   end
  end
end
for xv = 1,4 do --ready or not display
  for yv = 14,18 do
   if done == true then
	term.setCursorPos(xv,yv)
	term.write("+")
   else
	term.setCursorPos(xv,yv)
	term.write("")
   end
  end
end
term.setCursorPos(25,9)
term.write("x")
end
function leer(m) --reads the codons of a rednet message
local l = 3
local i = 1
repeat
  repeat
   if m:sub(l,1) ~= ":" then
	u[i] = u[i] + tonumber(m:sub(l,1))
   end
   l=l+1
  until m:sub(l,1) == ":"
  i=i+1
until m:sub(l,1) == ";"
end
function rd(m) --Reads the message
if m:sub(1,1) == 1 then --coordinate update message. Codons of four: x, y, level and value. Four codons at most.
  leer()
  for j = 1,4 do
   if u[1*j] ~= 0 then
	_G["a" .. tostring(u[3*j]) .. "[" .. tostring(u[1*j]) .. "][" .. tostring(u[2*j]) .. "]"] = u[4*j]
	u[1*j], u[2*j], u[3*j], u[4*j] = 0
   end
  end
end
if m:sub(1,1) == 2 then done = true end
du()
end
function move()
rednet.send(cid, "1")
done = false
end
function rt()
rednet.send(cid, "2")
done = false
end
function lt()
rednet.send(cid, "3")
done = false
end
function scan()
rednet.send(cid, "4")
done = false
end
function fullscan()
rednet.send(cid, "5")
done = false
end
function activate()
rednet.send(cid, "6")
done = false
end
function save(f)
if fs.exists("mine/map" .. tostring(f)) then fs.delete("mine/map" .. tostring(f)) end
local file = fs.open("mine/map" .. tostring(f), "w")
file.write(textutils.serialize(_G["a" .. tostring(f)]))
file.close()
end
function load(f)
local file = fs.open("mine/map" .. tostring(f), "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function ku()
if option - 4 >= 1 then
  option = option - 4
  du()
end
end
function kd()
if mo[menu] >= option + 4 then
  option = option + 4
  du()
end
end
function kr()
if mo[menu] >= option + 1 then
  option = option + 1
  du()
end
end
function kl()
if option - 1 >= 1 then
  option = option-1
  du()
end
end
function enter()
if menu == 1 then
  if option == 1 then --add turtle
   rednet.send(tonumber(read()), "98")
  elseif option == 2 then --turtle list from last session
   option = 1
   menu = 2
  elseif option == 3 then --load map from last session
   option = 1
   a11 = load(11)
   a12 = load(12)
   a13 = load(13)
   menu = 0
  elseif option == 4 then --quit the program
   option = 1
   menu = 3
  else
   menu = 0
  end
end
if menu == 2 then
  if option == lid + 1 then
   option = 1
   menu = 1
  end
end
if menu == 3 then
  if option == 1 then
   option = 1
   menu = 1
  else
   option = 1
   tm = 1
   menu = 0
  end
end
end
term.setCursorBlink(false)
rednet.open("right")
ia()
du()
repeat
local event, a, b, c = os.pullEvent()
if event == "rednet_Message" then
  rd(B)/>/>
end
if event == "key" then
  if a == 200 then
   if menu ~= 0 then
	ku()
   else
   cy = xy+1
   du()
   end
  end
  if a == 208 then
   if menu ~= 0 then
	kd()
   else
   cy =cy-1
   du()
   end
  end
  if a == 205 then
   if menu ~= 0 then
	kr()
   else
   cx = cx+1
   du()
   end
  end
  if a == 203 then
   if menu ~= 0 then
	kl()
   else
   cx =cy-1
   du()
   end
  end
  if a == 28  then
   if menu ~= 0 then
	menu = 1
   else
	enter()
   end
   du()
  end
  if cid ~= 0 and done == true then
   if a == 17 then
	move()
   end
   if a == 30 then
	lt()
   end
   if a == 32 then
	rt()
   end
   if a == 16 then
	scan()
   end
   if a == 18 then
	fullscan()
   end
   if a == 57 then
	activate()
   end
  end
end
while event == "mouse_drag" and a == 1 and menu == 0 do
  cx = b
  cy = c
  du()
end
if event == "mouse_click" and a == 2 and cid == 0 then
  for i = 1, lid do
   if _G["a"  ..  lvl .. "[" .. b .. "][" .. c .. "]"] == aid[i] then
	rednet.send(aid[i], "99")
	done = false
	cid = aid[i]
	du()
   end
  end
end
until (event == "key" and a == 207) or (tm == 1)
term.setCursorPos(1,1)
rednet.close("right")
term.clear()

EDIT: I fixed the problem with a new saving and loading format. I'm not saving the entire table, just the values that aren't 2.
Edited on 23 December 2013 - 11:39 PM