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

attempt to index ? (a nil value)

Started by bigwillanderson, 08 September 2012 - 09:17 PM
bigwillanderson #1
Posted 08 September 2012 - 11:17 PM
hello

I'm trying to use a computer to turn my reactor off an on again, but when I ran my program, it said
'attempt to index ? (a nil value)

this is my code

mon = peripheral.wrap("top")
mon.clear()
mon.setTextScale(1)
mon.write("REACTOR 1")

input = read()

if input == "turn off" then
redstone.setOutput("back", true)
mon.clear()
mon.write("turned off")
sleep(3)
mon.clear()
mon.write("REACTOR 1")
end

if input == "turn on" then
redstone.setOutput("back", false)
mon.clear()
mon.write("Turned on")
sleep(3)
mon.clear()
mon.write("REACTOR 1")
end
WildCard65 #2
Posted 08 September 2012 - 11:22 PM
Line number that the error is at?
Edit: Also this thread needs to be in Ask a Pro section.
bigwillanderson #3
Posted 08 September 2012 - 11:25 PM
oh sorry, I'm new, the line number is 2 I think
bigwillanderson #4
Posted 08 September 2012 - 11:27 PM
I put the coding in the startup bit and it says 'startup:2: attempt to index ? (a nil value)
MysticT #5
Posted 08 September 2012 - 11:34 PM
It looks like the monitor isn't on the top of the computer. In the first line check that the side you put is the one the monitor is on.
bigwillanderson #6
Posted 08 September 2012 - 11:46 PM
it's working now, but the monitor isn't displaying the text and the redstone isn't turning on :/
bigwillanderson #7
Posted 08 September 2012 - 11:48 PM
and do you know how I can program the computer to loop back, so I can turn the reactor off and turn it off again without rebooting?
MysticT #8
Posted 08 September 2012 - 11:55 PM
Here you go:

local mon = peripheral.wrap("top")
if not mon then
  print("Monitor not found")
  return
end

-- function to clear the monitor
local function clear()
  mon.clear()
  mon.setCursorPos(1, 1)
end

clear()
mon.setTextScale(1)
mon.write("REACTOR 1")

while true do -- infinite loop, will repeat the code inside forever (or until you break it or terminate the program)
  local input = read()
  if input == "turn off" then
    rs.setOutput("back", true)
    clear()
    mon.write("Turned OFF")
    sleep(3)
    clear()
    mon.write("REACTOR 1")
  elseif input == "turn on" then
    redstone.setOutput("back", false)
    clear()
    mon.write("Turned ON")
    sleep(3)
    clear()
    mon.write("REACTOR 1")
  end
end
bigwillanderson #9
Posted 09 September 2012 - 12:27 AM
thanks