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

Can Someone tell me what wrong with this program please :(

Started by tommy4eva, 13 January 2013 - 01:52 AM
tommy4eva #1
Posted 13 January 2013 - 02:52 AM
It says there is problem with Line 44 attempt to index ? (a nil Value) Can someone please help with this, its essentiall For aa server please reply ASAP, Yours sincerly Tommy4eva.
  1. mon = peripheral.wrap("left")
  2. function lineBreak()
  3. x, y = mon.getCursorPos()
  4. if y ~= 1 then
  5. y = y + 1
  6. end
  7. mon.setCursorPos(1, y)
  8. width, height = mon.getSize()
  9. mon.write("+" .. string.rep("-", width - 2) .. "+")
  10. end
  11. function printString(sString)
  12. x, y = mon.getCursorPos()
  13. y = y + 1
  14. mon.setCursorPos(1, y)
  15. mon.write(sString)
  16. end
  17. function printStringCentre(sString)
  18. x, y = mon.getCursorPos()
  19. y = y + 1
  20. mon.setCursorPos(1, y)
  21. width, height = mon.getSize()
  22. nStringCentre = math.floor(string.len(sString) / 2)
  23. nMonitorCentre = math.floor(width / 2)
  24. x = math.floor(nMonitorCentre - nStringCentre)
  25. mon.setCursorPos(x, y)
  26. mon.write(sString)
  27. end
  28. function printStringRight(sString)
  29. width, height = mon.getSize()
  30. x, y = mon.getCursorPos()
  31. y = y + 1
  32. x = math.ceil(width - string.len(sString))
  33. mon.setCursorPos(x, y)
  34. mon.write(sString)
  35. end
  36. function scrollText(tStrings, nRate)
  37. nRate = nRate or 5
  38. if nRate < 0 then
  39. error("rate must be positive")
  40. end
  41. end
  42. nSleep = 1 / nRate
  43. width, height = mon.getSize()
  44. x, y = mon.getCursorPos()
  45. sText = ""
  46. for n = 1, #tStrings do
  47. sText = sText .. tostring(tStrings[n])
  48. sText = sText .. " | "
  49. end
  50. sString = "| "
  51. if width / string.len(sText) < 1 then
  52. nStringRepeat = 3
  53. else
  54. nStringRepeat = math.ceil(width / string.len(sText) * 3)
  55. end
  56. for n = 1, nStringRepeat do
  57. sString = sString .. sText
  58. end
  59. while true do
  60. for n = 1, string.len(sText) do
  61. sDisplay = string.sub(sString, n, n + width - 1)
  62. mon.clearLine()
  63. mon.setCursorPos(1, y)
  64. mon.write(sDisplay)
  65. sleep(nSleep)
  66. end
  67. end
  68. mon.clear()
  69. mon.setCursorPos(1, 1)
  70. lineBreak()
  71. printStringCentre("|General stuff|")
  72. lineBreak()
  73. printString("")
  74. lineBreak()
  75. tScrollText = {}
  76. tScrollText[1] = "Fact:"
  77. tScrollText[2] = "Rainbowdash is NOT a girl!"
  78. tScrollText[3] = "Fact:"
  79. tScrollText[4] = "Grahamcraft is the best server ever in the world"
  80. lineBreak()
  81. printStringCentre("Rules")
  82. lineBreak()
  83. printString("1, Do not grief**")
  84. printString("2, Do not disrespect admins*")
  85. printString("4, Do not lie about donating to the server**")
  86. printString("5, Do not advertise other servers on ours***")
  87. printString("6, Do not steal from others**")
  88. printString("7, No pvp in this world")
  89. printString("8, Do not ignore these rules, READ THEM NOW!")
  90. printString("9, Play fair and have fun!***")
  91. lineBreak()
  92. printString(" * = warning")
  93. printString(" ** = depends how serious")
  94. printString(" *** = Instant ban")
  95. x, y = mon.getCursorPos()
  96. y = y - 1
  97. mon.setCursorPos(1, y)
  98. scrollText(tScrollText)
theoriginalbit #2
Posted 13 January 2013 - 03:02 AM
Firstly this is in the wrong section, should be in "Ask a Pro"

Secondly, thank you for putting line numbers, we also have code tags on this forum ;)/> :)/> [code][/code]

Lastly, is this copy pasted or typed out? because i don't see the issue with that line… if it really is that line then getSize() must not be available when using peripheral.wrap… alternatively you could change all the mon. to term. and use term.redirect at the start of the program and term.restore at the end of the program to get it printing on a monitor…

EDIT: Oops just occurred to me that i didn't tell you how to use redirect…


local mon = peripheral.wrap( "left" )
term.redirect( mon )


-- all your code here


term.restore()
Edited on 13 January 2013 - 02:04 AM
tommy4eva #3
Posted 13 January 2013 - 03:08 AM
thanks but now it says there is a problem with line 44… help
KaoS #4
Posted 13 January 2013 - 03:09 AM
that error means you are trying to access something in a table when the table does not actually exist, that code seems fine though as long as mon is not getting overwritten somewhere
tommy4eva #5
Posted 13 January 2013 - 03:16 AM
errm… could you tell me what this means, could you fix it for me, i dont know much about computer craft… :(/>
KaoS #6
Posted 13 January 2013 - 03:23 AM
there is no problem with the code above though…. it seems fine. do you have access the the world save folder of the server? if so get the actual program file from there and put it on pastebin, explain what you have set up (where the computer, monitor etc are in the MC world) and I should be able to set up a test world and get it sorted
remiX #7
Posted 13 January 2013 - 03:23 AM
Move line 44 to line 2 so it gets the size of the monitor as the wraps it.

Are you destroying the monitor while the program is running?
KaoS #8
Posted 13 January 2013 - 03:36 AM
even if you break the monitor you should not get that error, then the peripheral call would just fail
Nattiey1 #9
Posted 13 January 2013 - 04:04 AM
What's wrong with this then? (Tommy4evas friend)
I'm clueless about lua btw.
This code worked before but there was a tekkit update and it stops working.

mon = peripheral.wrap("left")
width, height = mon.getSize()
function lineBreak()
    x, y = mon.getCursorPos()
    if y ~= 1 then
		    y = y + 1
    end
    mon.setCursorPos(1, y)
    width, height = mon.getSize()
    mon.write("+" .. string.rep("-", width - 2) .. "+")
end
function printString(sString)
    x, y = mon.getCursorPos()
    y = y + 1
    mon.setCursorPos(1, y)
    mon.write(sString)
end
function printStringCentre(sString)
    x, y = mon.getCursorPos()
    y = y + 1
    mon.setCursorPos(1, y)
    width, height = mon.getSize()
    nStringCentre = math.floor(string.len(sString) / 2)
    nMonitorCentre = math.floor(width / 2)
    x = math.floor(nMonitorCentre - nStringCentre)
    mon.setCursorPos(x, y)
    mon.write(sString)
end
function printStringRight(sString)
width, height = mon.getSize()
x, y = mon.getCursorPos()
y = y + 1
x = math.ceil(width - string.len(sString))
mon.setCursorPos(x, y)
mon.write(sString)
end
function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end
end
nSleep = nRate
x, y = mon.getCursorPos()
    sText = "fds"
    for n = 1, #tStrings do
		    sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
    end
    sString = "| "
    if width / string.len(sText) < 1 then
		    nStringRepeat = 3
    else
		    nStringRepeat = math.ceil(width / string.len(sText) * 3)
    end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
mon.clear()
mon.setCursorPos(1, 1)
lineBreak()
printStringCentre("|General stuff|")
lineBreak()
printString("")
lineBreak()
tScrollText = {}
tScrollText[1] = "Fact:"
tScrollText[2] = "Rainbowdash is NOT a girl!"
tScrollText[3] = "Fact:"
tScrollText[4] = "Grahamcraft is the best server ever in the world"
lineBreak()
printStringCentre("Rules")
lineBreak()
printString("1, Do not grief**")
printString("2, Do not disrespect admins*")
printString("4, Do not lie about donating to the server**")
printString("5, Do not advertise other servers on ours***")
printString("6, Do not steal from others**")
printString("7, No pvp in this world")
printString("8, Do not ignore these rules, READ THEM NOW!")
printString("9, Play fair and have fun!***")
lineBreak()
printString(" * = warning")
printString(" ** = depends how serious")
printString(" *** = Instant ban")
x, y = mon.getCursorPos()
y = y - 1
mon.setCursorPos(1, y)
scrollText(tScrollText)
KaoS #10
Posted 13 January 2013 - 04:06 AM
error please
Nattiey1 #11
Posted 13 January 2013 - 04:09 AM

general:46: attempt to get length of nil
remiX #12
Posted 13 January 2013 - 04:25 AM
Try changing the scrollText function to this?

function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end

nSleep = nRate
x, y = mon.getCursorPos()
    sText = "fds"
    for n = 1, #tStrings do
                    sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
    end
    sString = "| "
    if width / string.len(sText) < 1 then
                    nStringRepeat = 3
    else
                    nStringRepeat = math.ceil(width / string.len(sText) * 3)
    end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
end

You had an end in the wrong place causing the function to end after the first if statement
Skullblade #13
Posted 13 January 2013 - 04:25 AM
what line is 46…
Nattiey1 #14
Posted 13 January 2013 - 06:52 AM
I'm getting a new error now

Here's the current code

mon = peripheral.wrap("left")
width, height = mon.getSize()
function lineBreak()
	x, y = mon.getCursorPos()
	if y ~= 1 then
			y = y + 1
	end
	mon.setCursorPos(1, y)
	width, height = mon.getSize()
	mon.write("+" .. string.rep("-", width - 2) .. "+")
end
function printString(sString)
	x, y = mon.getCursorPos()
	y = y + 1
	mon.setCursorPos(1, y)
	mon.write(sString)
end
function printStringCentre(sString)
	x, y = mon.getCursorPos()
	y = y + 1
	mon.setCursorPos(1, y)
	width, height = mon.getSize()
	nStringCentre = math.floor(string.len(sString) / 2)
	nMonitorCentre = math.floor(width / 2)
	x = math.floor(nMonitorCentre - nStringCentre)
	mon.setCursorPos(x, y)
	mon.write(sString)
end
function printStringRight(sString)
width, height = mon.getSize()
x, y = mon.getCursorPos()
y = y + 1
x = math.ceil(width - string.len(sString))
mon.setCursorPos(x, y)
mon.write(sString)
end
function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end

nSleep = nRate
x, y = mon.getCursorPos()
	sText = "fds"
	for n = 1, #tStrings do
					sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
	end
	sString = "| "
	if width / string.len(sText) < 1 then
					nStringRepeat = 3
	else
					nStringRepeat = math.ceil(width / string.len(sText) * 3)
	end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
end
mon.clear()
mon.setCursorPos(1, 1)
lineBreak()
printStringCentre("|General stuff|")
lineBreak()
printString("")
lineBreak()
tScrollText = {}
tScrollText[1] = "Fact:"
tScrollText[2] = "R"
tScrollText[3] = "Fact:"
tScrollText[4] = "G"
lineBreak()
printStringCentre("Rules")
lineBreak()
printString("1,")
printString("2,")
printString("4,")
printString("5,")
printString("6,")
printString("7,")
printString("8,")
printString("9,")
lineBreak()
printString("g")
printString("s")
printString("h")
x, y = mon.getCursorPos()
y = y - 1
mon.setCursorPos(1, y)
scrollText(tScrollText)


Error:

general:2:attempt to call index ?(a nil value)
Skullblade #15
Posted 13 January 2013 - 06:57 AM
I just ran the program and it works fine…the only reason you would get that error is if there isn't a monitor on the left side
Nattiey1 #16
Posted 13 January 2013 - 07:04 AM
Ah that would make sense, thanks.
I'm being a bit stupid today :P/>
Skullblade #17
Posted 13 January 2013 - 07:14 AM
its fine…just make sure your using monitors not computers :P/>