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

Issue with tables being nil

Started by micmou, 18 May 2014 - 04:51 AM
micmou #1
Posted 18 May 2014 - 06:51 AM
I'm having a problem with the Mall Directory i'm making for a server i'm an admin on. it "worked" until i added the code at the top to save the data in a file. There is another computer that allows admins to edit the shop name on the screen which was working again till i added the file function to my code. Any help would be greatly appreciated. here is a picture of the directory.(http://prntscr.com/3k8t2w) This code is only for one screen i have not yet change the others to this set up they only get there info when entered from main comp and lose their info if restart.


monitor = peripheral.wrap("top")
monitor.clear()
monitor.setTextScale(2)
monitor.setCursorPos(14,1)
monitor.setTextColor(colors.red)
monitor.write("First Floor")
monitor.setTextColor(colors.lightBlue)
rednet.open ("left")



function check()
  if not fs.exists("shopFloor") then
    local file = fs.open("shopFloor", "w")
    file.write()
    file.close()
  end
end

function readf()
  local file = fs.open("shopFloor", "r")
  shopTable = file.readAll()
  file.close()
  return textutils.unserialize(shopTable)
end

function savef()
  local file = fs.open("shopFloor", "w")
  textutils.serialize(shopTable)
  file.write(shopTable)
  file.close()
end





local shopTable = ""
local file = ""
local event = ""
local shopName1 = shopTable[1]
local shopName2 = shopTable[2]
local shopName3 = shopTable[3]
local shopName4 = shopTable[4]
local shopName5 = shopTable[5]
local shopName6 = shopTable[6]
local shopName7 = shopTable[7]
local shopName8 = shopTable[8]
local shopName9 = shopTable[9]
local shopName10 = shopTable[10]
local shopName11 = shopTable[11]
local shopName12 = shopTable[12]

check()
while true do
readf()

monitor.setCursorPos(2, 2)
monitor.clearLine()
monitor.write("101 - "..tostring(shopName1))
monitor.setCursorPos(2, 3)
monitor.clearLine()
monitor.write("102 - "..tostring(shopName2))
monitor.setCursorPos(2, 4)
monitor.clearLine()
monitor.write("103 - "..tostring(shopName3))
monitor.setCursorPos(2, 5)
monitor.clearLine()
monitor.write("104 - "..tostring(shopName4))
monitor.setCursorPos(2, 6)
monitor.clearLine()
monitor.write("105 - "..tostring(shopName5))
monitor.setCursorPos(2, 7)
monitor.clearLine()
monitor.write("106 - "..tostring(shopName6))
monitor.setCursorPos(2, 8)
monitor.clearLine()
monitor.write("107 - "..tostring(shopName7))
monitor.setCursorPos(2, 9)
monitor.clearLine()
monitor.write("108 - "..tostring(shopName8))
monitor.setCursorPos(2, 10)
monitor.clearLine()
monitor.write("109 - "..tostring(shopName9))
monitor.setCursorPos(2, 11)
monitor.clearLine()
monitor.write("110 - "..tostring(shopName10))
monitor.setCursorPos(2, 12)
monitor.clearLine()
monitor.write("111 - "..tostring(shopName11))
monitor.setCursorPos(2, 13)
monitor.clearLine()
monitor.write("112 - "..tostring(shopname12))

id, msg = rednet.receive()
table = textutils.unserialize(msg)
id = table[1]
text = table[2]
event = os.pullEvent()
if event == "rednet_message" then
    if id == 101 then
	  shopName1 = text
	  monitor.setCursorPos(2, 2)
	  monitor.clearLine()
	  monitor.write("101 - " .. shopName1)
    elseif id == 102 then
	  shopName2 = text
	  monitor.setCursorPos(2, 3)
	  monitor.clearLine()
	  monitor.write("102 - " .. shopName2)
    elseif id == 103 then
	  shopName3 = text
	  monitor.setCursorPos(2, 4)
	  monitor.clearLine()
	  monitor.write("103 - " .. shopName3)
    elseif id == 104 then
	  shopName4 = text
	  monitor.setCursorPos(2, 5)
	  monitor.clearLine()
	  monitor.write("104 - " .. shopName4)
    elseif id == 105 then
	  shopName5 = text
	  monitor.setCursorPos(2, 6)
	  monitor.clearLine()
	  monitor.write("105 - " .. shopName5)
    elseif id == 106 then
	  shopName6 = text
	  monitor.setCursorPos(2, 7)
	  monitor.clearLine()
	  monitor.write("106 - " .. shopName6)
    elseif id == 107 then
	  shopName7 = text
	  monitor.setCursorPos(2, 8)
	  monitor.clearLine()
	  monitor.write("107 - " .. shopName7)
    elseif id == 108 then
	  shopName8 = text
	  monitor.setCursorPos(2, 9)
	  monitor.clearLine()
	  monitor.write("108 - " .. shopName8)
    elseif id == 109 then
	  shopName9 = text
	  monitor.setCursorPos(2, 10)
	  monitor.clearLine()
	  monitor.write("109 - " .. shopName9)
    elseif id == 110 then
	  shopName10 = text
	  monitor.setCursorPos(2, 11)
	  monitor.clearLine()
	  monitor.write("110 - " .. shopName10)
    elseif id == 111 then
	  shopName11 = text
	  monitor.setCursorPos(2, 12)
	  monitor.clearLine()
	  monitor.write("111 - " .. shopName11)
    elseif id == 112 then
	  shopName12 = text
	  monitor.setCursorPos(2, 13)
	  monitor.clearLine()
	  monitor.write("112 - " .. shopName12)
  end
end
savef()
end
Edited on 18 May 2014 - 04:52 AM
wieselkatze #2
Posted 18 May 2014 - 08:36 AM
First you declare shopTable as a string (shopTable = ""), so when you set your variables (shopName1 etc.) there isn't a table to read from.
Your declaration for a table is in the actual loop, but the variables actuwlly never get updated.
Putting the readf() directly after the local shopTable = "" should fix it.
augustas656 #3
Posted 18 May 2014 - 04:01 PM
You should really try doing for loops.