Wolverine1621
Wolverine1621

Reputation: 295

Lua "Attempt to index ? (a nil value)

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).

Upvotes: 1

Views: 15110

Answers (6)

Freya
Freya

Reputation: 324

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..")

Upvotes: 2

aitc-h
aitc-h

Reputation: 1

Try using term.clear() instead of m.clear().

Upvotes: 0

Alan Doyle
Alan Doyle

Reputation: 108

try to check to see if it is nil first or not..

if m ~= nil then
  m.clear()
else 
 print("can't connect")
end

Upvotes: 0

Asego
Asego

Reputation: 1

I think the first line should be "local m =..." Also, peripheral.wrap("right") means it makes the modem, printer, etc on the right of the computer usable by sending any commands there instead of trying to run it on the computer.

Upvotes: 0

Ewildawe
Ewildawe

Reputation: 11

Is this on a monitor?

If yes, then the monitor has to be to the right of the computer. m.clear() is actually understood as peripheral.wrap("right").clear() and will access the monitor to the right and use the command clear().

If you are trying to clear this on the computer simply use term.clear().

Upvotes: 1

meso
meso

Reputation: 1

instead of m.clear() you should use term.clear()

Upvotes: 0

Related Questions