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

weird bug ?

Started by exporta22, 13 March 2016 - 01:22 AM
exporta22 #1
Posted 13 March 2016 - 02:22 AM
im trying to monitorize my oxygen and energy cell on a computer in the galacticraft modpack. when im trying the code in solo, it work but in mutliplayer it say :

111:88: attempt to index ? (a nil value)

here's the code

on my energy cell:

rednet.open("top")
Cell = peripheral.wrap("bottom")
while true do
  local a = Cell.getEnergyStored()
  local b = Cell.getMaxEnergyStored()

  local a2 = tostring(a)
  local b2 = tostring(B)/>/>
  local donnees = { a2, b2}
  local encode = textutils.serialise(donnees)

  rednet.send(40, encode)
  term.setCursorPos(1,1)
  term.clear()
  print("Analyse de la Cell en cours...")
  sleep(0)
end

and on the computer who will monitorize everything:


rednet.open("left")
local mon = peripheral.wrap("top")
term.clear()
monX, monY = mon.getSize()

function clear()
  mon.setBackgroundColor(colors.black)
  mon.clear()
  mon.setCursorPos(1,1)
end

function drawText(x, y, text, color_txt, color_bg)
  mon.setBackgroundColor(color_bg)
  mon.setTextColor(color_txt)
  mon.setCursorPos(x,y)
  mon.write(text)
end

function drawLine(x, y, length, size, color_bar)
  for yPos = y, y+size-1 do
	mon.setBackgroundColor(color_bar)
	mon.setCursorPos(x, yPos)
	mon.write(string.rep(" ", length))
  end
end

function drawProg(x, y, name, length, size, minVal, maxVal, color_bar, color_bg)
  drawLine(x, y, length, size, color_bg)
  local barSize = math.floor((minVal/maxVal)*length)
  drawLine(x, y, barSize, size, color_bar)
  local text = name.." "..math.floor((minVal/maxVal)*100).."%"
  if barSize > length/2+#text/2 then
	drawText(x+length/2-#text/2, y+size/2, text, colors.black, color_bar)
  elseif barSize > #text then
	drawText((x+barSize)-#text, y+size/2, text, colors.black, color_bar)
  else
	drawText(x+length/2-#text/2, y+size/2, text, colors.black, color_bg)
  end
end
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setTextColor(colors.lightBlue)
mon.setCursorPos(8,1)
mon.write("Gestion d'oxygen")
mon.setTextColor(colors.white)
mon.setCursorPos(1,3)
mon.write("Centrale")
mon.setCursorPos(1,4)
mon.write("SAS")
mon.setCursorPos(1,5)
mon.write("corridor SAS")
mon.setCursorPos(1,6)
mon.write("Corridor utilite")
mon.setCursorPos(1,7)
mon.write("Corridor oxygen")
mon.setCursorPos(1,8)
mon.write("Corridor energie")
mon.setCursorPos(1,9)
mon.write("Reacteur")
mon.setCursorPos(1,10)
mon.write("Batterie")
--mon.setCursorPos(1,11)
--mon.write("Quarters")
--mon.setCursorPos(1,12)
--mon.write("Cartography")
--mon.setCursorPos(1,13)
--mon.write("Nuclear Core")
mon.setCursorPos(1,14)
mon.write("------------------------------------")
mon.setCursorPos(6,15)
mon.setTextColor(colors.orange)
mon.write("Cellule  d'energie")
mon.setCursorPos(1,17)
mon.setTextColor(colors.white)
mon.write("nombre de RF:")
term.setCursorPos(1,1)
print("OXYGEN SYSTEMS")

while true do
  local event, id, text = os.pullEvent("rednet_message")

  if event == "rednet_message" then
	recup = textutils.unserialise(text)
  end
  local a = recup[1]   --line 88
  local b = recup[2]   --line 89
  local c = tonumber(a)
  if c > 1000 and c < 1000000 then
	mon.setBackgroundColor(colors.black)
	mon.setTextColor(colors.lightBlue)
	mon.setCursorPos(6, 18)
	mon.write("K")
	mon.setCursorPos(1, 18)
	c = math.floor(a/1000)
	mon.write(c)
  elseif c > 1000000 then
	mon.setBackgroundColor(colors.black)
	mon.setTextColor(colors.lightBlue)
	mon.setCursorPos(5, 18)
	mon.write("M")
	mon.setCursorPos(1, 18)
	c = math.floor(a/1000000)
	mon.write(c)
  end	
  i = ((a/B)/>/>*100)
  drawProg(1,19, " ", 15, 1, i, 100, colors.green, colors.gray)
  sleep(0.2)
if rs.getInput("left") == true then
mon.setCursorPos(15,3)
mon.setBackgroundColor(colors.lime)
mon.write("ON ")
end

if rs.getInput("left") == false then
mon.setCursorPos(15,3)
mon.setBackgroundColor(colors.red)
mon.write("OFF")
end

local id, message = rednet.receive()
if message == "o7" and id == 37 then
mon.setCursorPos(15,4)
mon.setBackgroundColor(colors.cyan)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o7n" and id == 37 then
mon.setCursorPos(15,4)
mon.setBackgroundColor(colors.orange)
mon.write("OFF")
end

local id, message = rednet.receive()
if message == "o6" and id == 36 then
mon.setCursorPos(15,5)
mon.setBackgroundColor(colors.lime)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o6n" and id == 36 then
mon.setCursorPos(15,5)
mon.setBackgroundColor(colors.red)
mon.write("OFF")
end

local id, message = rednet.receive()
if message == "o5" and id == 35 then
mon.setCursorPos(15,6)
mon.setBackgroundColor(colors.cyan)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o5n" and id == 35 then
mon.setCursorPos(15,6)
mon.setBackgroundColor(colors.orange)
mon.write("OFF")
end

local id, message = rednet.receive()
if message == "o4" and id == 34 then
mon.setCursorPos(15,7)
mon.setBackgroundColor(colors.lime)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o4n" and id == 34 then
mon.setCursorPos(15,7)
mon.setBackgroundColor(colors.red)
mon.write("OFF")
end

local id, message = rednet.receive()
if message == "o1" and id == 32 then
mon.setCursorPos(15,8)
mon.setBackgroundColor(colors.cyan)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o1n" and id == 32 then
mon.setCursorPos(15,8)
mon.setBackgroundColor(colors.orange)
mon.write("OFF")
end	

local id, message = rednet.receive()
if message == "o3" and id == 33 then
mon.setCursorPos(15,9)
mon.setBackgroundColor(colors.lime)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o3n" and id == 33 then
mon.setCursorPos(15,9)
mon.setBackgroundColor(colors.red)
mon.write("OFF")
end

local id, message = rednet.receive()
if message == "o2" and id == 31 then
mon.setCursorPos(15,10)
mon.setBackgroundColor(colors.cyan)
mon.write("ON ")
end

local id, message = rednet.receive()
if message == "o2n" and id == 31 then
mon.setCursorPos(15,10)
mon.setBackgroundColor(colors.orange)
mon.write("OFF")
end
end

thanx for the help
Edited on 13 March 2016 - 01:24 AM
Dragon53535 #2
Posted 13 March 2016 - 06:05 AM
make sure that recup isn't nil, which it is if you receive a broadcast message sent by someone else.

if recup then
  --#All of your code that is line 88 and below that relies on recup
end
exporta22 #3
Posted 13 March 2016 - 02:03 PM
i just tested it and it bypass simply the code of "recup"
exporta22 #4
Posted 13 March 2016 - 02:39 PM
ok i fixed it now!

i use

while true do
  local id, message = rednet.receive()
  if id == 41 then

    recup = textutils.unserialise(message)
    local a = recup[1]

instead of


while true do
  local event, id, text = os.pullEvent("rednet_message")
  if event == "rednet_message" then
	    recup = textutils.unserialise(text)
  end
  local a = recup[1]