Lua "Attempt to index ? (a nil value)

Wolverine1621 picture Wolverine1621 · Mar 11, 2013 · Viewed 12.7k times · Source

So my friend and I are trying to write a program for ComputerCraft (Minecraft Mod), which uses Lua as a programming language. I haven't done any Lua before and he's played around a bit with it. Basically, we're trying to clear a line of text with m.clear(), but I think that it may not know what m, is, even though I tried to define it.

Sorry if the question is poorly worded, here's the code:

m = peripheral.wrap("right")
m.write("Shutting down.")
m.clear()
sleep(.1)
m.setcursorpos(1,1)
print("Shutting Down..")

And the function of the rest of the code (which is just more of the same) I won't post, because the function of the program is to make it so that it'll add a . each time, if you understand what I mean. But, that's not the important part. :)

Notes: I don't actually know what peripheral.wrap("right") means, it was taken from the ComputerCraft forums from another person's code (he also wanted to clear the screen).

Answer

Fre_d picture Fre_d · Jun 1, 2014

The function peripheral.wrap(string side) returns a table containing some functions from the peripheral at the specified side of the computer (if none is found this returns nil). You got the error because you tried to reach the variable in a nil (nil = nothing).

Calling the following will print Hello, World to the front of the monitor:

monitor = peripheral.wrap("top")
monitor.print("Hello, World")

Similarly you can do the same with your terminal (the computer gui, you don't have to wrap it):

term.print("Hello, World")

If you just want to interact with the gui (and not an external monitor), you want to use the table term instead of peripheral.wrap().

term.write("Shutting down.")
term.clear()
sleep(.1)
term.setCursorPos(1,1) --This is supposed to be setCursorPos not setcursorpos!!!
print("Shutting Down..")