←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Lua Error when making Monitor Program

TheHackerPlayer's Photo TheHackerPlayer 28 Jul 2015

Hello, I have an error when I try to execute my short but simple lua program. It says "writemonitor:14: attempt to call nil" I have been trying to figure this out for 25 minutes but not a clue. Any help?

EDIT: Changed code around a little.. By using the if check.
EDIT2: Spelling. Also, see new code at bottom of post.

Pastebin for finished code: Here

term.clear() --#Clear any previous text
up = "peripheral.wrap(top)"
down = "peripheral.wrap(bottom)"
left = "peripheral.wrap(left)"
right = "peripheral.wrap(right)"
term.setCursorPos(18,1) --#Center text to terminal top
print("What direction?") --#Calls perhipherals
	if read() = "up" then up
	if read() = "down" then down
	if read() = "left" then left
	if read() = "right" then right
end
term.clear() --#Clear any previous text again
term.setCursorPos(13,1) --#Center text to terminal top
print("What Would You Like To Say?") --#Prompts user for text input
t = read()
monitor.write(t) --# I need help here

term.clear()
up = peripheral.wrap("top")
term.setCursorPos(20,1)
print("Direction?")
d = string.lower(read())
if d == "up" then
  monitor = up
elseif d == "down" then
  monitor = down
elseif d == "right" then
  monitor = right
elseif d == "left" then
  monitor = left
end
term.setCursorPos(12,1)
term.clear()
print("What Would You Like To Say?")
t = read()
monitor.write(t)

Edited by TheHackerPlayer, 28 July 2015 - 05:18 PM.
Quote

KingofGamesYami's Photo KingofGamesYami 28 Jul 2015

Actually, the error should be
writemonitor:14:attempt to index ? (a string value)

..Because you are trying to index a string on line 14.

term.clear() --#clear the terminal
up = "perhiperal.wrap(top)" --#make some useless strings (consider actually calling peripheral.wrap)
down = "perhiperal.wrap(bottom)"
left = "perhiperal.wrap(left)"
right = "perhiperal.wrap(right)"
term.setCursorPos(18,1) --#set the cursor position
print("What direction?")
d = read() --#get a string input from the user
term.clear() --#clear the terminal again
term.setCursorPos(13,1) --#move the cursor again
print("What Would You Like To Say?")
t = read() --#get another string input from the user
monitor = d --#set a variable equal to another variable (redundant and useless)
monitor.write(t) --#treat the earlier string you got from the user as if it were a wrapped monitor object.  Which it is not.
Quote

TheHackerPlayer's Photo TheHackerPlayer 28 Jul 2015

 KingofGamesYami, on 28 July 2015 - 03:52 PM, said:

Actually, the error should be
writemonitor:14:attempt to index ? (a string value)

..Because you are trying to index a string on line 14.

term.clear() --#clear the terminal
up = "perhiperal.wrap(top)" --#make some useless strings (consider actually calling peripheral.wrap)
down = "perhiperal.wrap(bottom)"
left = "perhiperal.wrap(left)"
right = "perhiperal.wrap(right)"
term.setCursorPos(18,1) --#set the cursor position
print("What direction?")
d = read() --#get a string input from the user
term.clear() --#clear the terminal again
term.setCursorPos(13,1) --#move the cursor again
print("What Would You Like To Say?")
t = read() --#get another string input from the user
monitor = d --#set a variable equal to another variable (redundant and useless)
monitor.write(t) --#treat the earlier string you got from the user as if it were a wrapped monitor object.  Which it is not.

Hey, I am new to lua and Computercraft. I think i know what my error is, but don't know how to fix. I'm looking how to fix, and what caused the error not some comment of what I did. Thanks for trying to help me I guess?
Quote

Dog's Photo Dog 28 Jul 2015

Welcome to the CC forums, TheHackerPlayer.

I noticed a few errors in your code. The first is that you spelled peripheral incorrectly for all your wraps and you aren't actually wrapping anything since you're defining your variables as strings (by putting their values in quotes).

Also, read() returns a string and peripheral wraps are essentially variables that point to tables, not strings, so you can't use your read() input to directly select a monitor like you're trying to do.

You might want to try a simple, straight forward approach like this...
term.clear()
up = peripheral.wrap("top") --# note that the only string we use here is to define which side the monitor is on
... --# rest of top code here
d = string.lower(read()) --# we convert our input to lower case so comparison is easier
if d == "up" then --# now we compare our input to find what the user selected and set the monitor variable to point to the correct wrapped monitor
  monitor = up
elseif d == "down" then
  monitor = down
elseif d == "right" then
  monitor = right
elseif d == "left" then
  monitor = left
end
... --# rest of bottom code here
t = read()
monitor.write(t)

This won't prevent someone from entering an invalid side or handle misspellings, etc., so keep that in mind, but this should provide a basic framework to get you started.
Edited by Dog, 28 July 2015 - 04:26 PM.
Quote

Cranium's Photo Cranium 28 Jul 2015

Calm the flip down. He did exactly that, showing where your error was.
Quote

TheHackerPlayer's Photo TheHackerPlayer 28 Jul 2015

 Dog, on 28 July 2015 - 04:22 PM, said:

Welcome to the CC forums, TheHackerPlayer.

I noticed a few errors in your code. The first is that you spelled peripheral incorrectly for all your wraps and you aren't actually wrapping anything since you're defining your variables as strings (by putting their values in quotes).

Also, read() returns a string and peripheral wraps are essentially variables that point to tables, not strings, so you can't use your read() input to directly select a monitor like you're trying to do.

You might want to try a simple, straight forward approach like this...
term.clear()
up = peripheral.wrap("top") --# note that the only string we use here is to define which side the monitor is on
... --# rest of top code here
d = string.lower(read()) --# we convert our input to lower case so comparison is easier
if d == "up" then --# now we compare our input to find what the user selected and set the monitor variable to point to the correct wrapped monitor
  monitor = up
elseif d == "down" then
  monitor = down
elseif d == "right" then
  monitor = right
elseif d == "left" then
  monitor = left
end
... --# rest of bottom code here
t = read()
monitor.write(t)

This won't prevent someone from entering an invalid side or handle misspellings, etc., so keep that in mind, but this should provide a basic framework to get you started.

Thanks. Now I see my error. I am still newb at lua and improving. This is my first lua script....

 Cranium, on 28 July 2015 - 04:23 PM, said:

Calm the flip down. He did exactly that, showing where your error was.

I just noticed he fixed my spelling, string points to table, and more. I'm still newb, so i kinda know what they're talking about.

 KingofGamesYami, on 28 July 2015 - 03:52 PM, said:

Actually, the error should be
writemonitor:14:attempt to index ? (a string value)

..Because you are trying to index a string on line 14.

term.clear() --#clear the terminal
up = "perhiperal.wrap(top)" --#make some useless strings (consider actually calling peripheral.wrap)
down = "perhiperal.wrap(bottom)"
left = "perhiperal.wrap(left)"
right = "perhiperal.wrap(right)"
term.setCursorPos(18,1) --#set the cursor position
print("What direction?")
d = read() --#get a string input from the user
term.clear() --#clear the terminal again
term.setCursorPos(13,1) --#move the cursor again
print("What Would You Like To Say?")
t = read() --#get another string input from the user
monitor = d --#set a variable equal to another variable (redundant and useless)
monitor.write(t) --#treat the earlier string you got from the user as if it were a wrapped monitor object.  Which it is not.

Umm, I just noticed that you fixed my spelling error, useless stuff and more. Sorry if it seemed like I was yelling at you, I just didn't understand (I'm a newb) and I got confused.
Edited by Cranium, 28 July 2015 - 05:16 PM.
Quote

KingofGamesYami's Photo KingofGamesYami 28 Jul 2015

It's ok. I'm not offended, the goal of my post was not to tell you how to fix your error, but to get you to understand why it erred. After you understand why something doesn't work correctly, you can avoid such situations in the future.
Quote

TheHackerPlayer's Photo TheHackerPlayer 28 Jul 2015

 Dog, on 28 July 2015 - 04:22 PM, said:

Welcome to the CC forums, TheHackerPlayer.

I noticed a few errors in your code. The first is that you spelled peripheral incorrectly for all your wraps and you aren't actually wrapping anything since you're defining your variables as strings (by putting their values in quotes).

Also, read() returns a string and peripheral wraps are essentially variables that point to tables, not strings, so you can't use your read() input to directly select a monitor like you're trying to do.

You might want to try a simple, straight forward approach like this...
term.clear()
up = peripheral.wrap("top") --# note that the only string we use here is to define which side the monitor is on
... --# rest of top code here
d = string.lower(read()) --# we convert our input to lower case so comparison is easier
if d == "up" then --# now we compare our input to find what the user selected and set the monitor variable to point to the correct wrapped monitor
  monitor = up
elseif d == "down" then
  monitor = down
elseif d == "right" then
  monitor = right
elseif d == "left" then
  monitor = left
end
... --# rest of bottom code here
t = read()
monitor.write(t)

This won't prevent someone from entering an invalid side or handle misspellings, etc., so keep that in mind, but this should provide a basic framework to get you started.


throws error when the direction is not up. Help?
Edited by TheHackerPlayer, 28 July 2015 - 05:54 PM.
Quote

Dog's Photo Dog 28 Jul 2015

The code I provided was not complete - it was designed to point you in the right direction. The idea is that you take what I wrote, merge in the relevant parts of your code and you should have a working program. Take another look at what I wrote and (based on the comments) try to figure out which code of yours needs to be added to make the program complete. If you run into problems go ahead and post what you come up with and we'll go from there....
Quote