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

Attempt to index ?(a nil value)

Started by HeyImZach, 18 May 2014 - 05:36 AM
HeyImZach #1
Posted 18 May 2014 - 07:36 AM
I am making a notice board and i keep getting an error:


noticeBoard: 7: attempt to index ?(a nil value)

here is my code:

status = "Noting important going on." --can be changed
mon = peripheral.wrap("back") --replace with side that the monitor is on
mon.setTextScale(2) --textsize
term.redirect(mon) --redirects the terminal to the monitor
term.clear()
term.setCursorPos(1,1)
center.PrintCenter "Welcome to the Nexus!"
os.sleep(0.5)
center.PrintCenter "A teleportation hub" 
center.PrintCenter "When it's built :P/>"
os.sleep(0.5)
center.PrintCenter(status) --prints that status message at the start
os.sleep(60) --waits for a min
term.restore()
Lyqyd #2
Posted 18 May 2014 - 12:01 PM
Moved to Ask a Pro.
CometWolf #3
Posted 18 May 2014 - 12:05 PM
You're code dosen't define a table called center

center.PrintCenter "Welcome to the Nexus!"
Hence the error "Attempt to index nil" when attempting to index the nil variable center
TheOddByte #4
Posted 18 May 2014 - 04:25 PM
As CometWolf said, there's no table called center.
You could ofcourse fix it like this

local center = {
	PrintCenter = function( text )
		local w, h = term.getSize()
		local x, y = term.getCursorPos()
		term.setCursorPos( math.ceil(w/2 - #text/2), y )
		term.write( text )
	end;
}

center.PrintCenter("Test") -- Test it
Edited on 18 May 2014 - 02:26 PM
HeyImZach #5
Posted 18 May 2014 - 11:12 PM
As CometWolf said, there's no table called center.
You could ofcourse fix it like this

local center = {
	PrintCenter = function( text )
		local w, h = term.getSize()
		local x, y = term.getCursorPos()
		term.setCursorPos( math.ceil(w/2 - #text/2), y )
		term.write( text )
	end;
}

center.PrintCenter("Test") -- Test it

Thanks that fixed it! But what about this code? I'm getting the same error.


mon = peripheral.wrap("right")
local width, height = mon.getSize()

function setup()
  mon.clear()
  mon.setCursorPos(1,1)
end

divheight = height/2
midheight = math.floor(divheight)

	
function clickScreen()
  while true do
	event, side, xPos, yPos = os.pullEvent("monitor_touch")
	mon.setCursorPos(xPos, yPos)
	if yPos == height then
	  mon.setBackgroundColor(colors.black)
	  mon.clear()
	  clearBar()
	else
	  alertOpen()
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Enter text:")
	  input = read()
	  alertClose()
	  mon.setCursorPos(xPos, yPos)
	  mon.write(input)
	  term.clear()
	end
  end
end

function clearBar()
  mon.setCursorPos(1,height)
  mon.setBackgroundColor(colors.red)
  mon.clearLine()
  mon.write("[Clear]")
  mon.setBackgroundColor(colors.black)
end

function alertOpen()
  mon.setCursorPos(5,midheight)
  mon.setBackgroundColor(8)
  mon.clearLine()
  mon.write("Type in message on monitor")
end

function alertClose()
  mon.setCursorPos(5,midheight)
  mon.setBackgroundColor(colors.black)
  mon.clearLine()
end

setup()
clearBar()
clickScreen()

Nevermind. Just put wrong side in code :)/>