Jump to content




Lua Error when making Monitor Program

computer help peripheral

8 replies to this topic

#1 TheHackerPlayer

  • Members
  • 6 posts

Posted 28 July 2015 - 03:34 PM

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.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 July 2015 - 03:52 PM

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.


#3 TheHackerPlayer

  • Members
  • 6 posts

Posted 28 July 2015 - 04:17 PM

View PostKingofGamesYami, 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?

#4 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

Posted 28 July 2015 - 04:22 PM

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.


#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 July 2015 - 04:23 PM

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

#6 TheHackerPlayer

  • Members
  • 6 posts

Posted 28 July 2015 - 04:31 PM

View PostDog, 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....

View PostCranium, 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.

View PostKingofGamesYami, 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.


#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 July 2015 - 05:20 PM

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.

#8 TheHackerPlayer

  • Members
  • 6 posts

Posted 28 July 2015 - 05:22 PM

View PostDog, 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.


#9 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

Posted 28 July 2015 - 05:59 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users