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

Menu giving "Attempting to index ? (A nil value)"

Started by GamersUnited556, 31 October 2017 - 11:52 PM
GamersUnited556 #1
Posted 01 November 2017 - 12:53 AM
https://pastebin.com/QWDcpwFE

I have been trying to impliment a menu selection method into a program with a password lock,
so far I have gotten errors every time I try and press a key when it hits the menu
Line 48 to be exact, I don't know what's happening.

Should be noted running CC 1.41 (MC 1.2.5)
Dave-ee Jones #2
Posted 01 November 2017 - 02:28 AM
I noticed a few things:

1. Your Exit() function is calling on a function called 'c1', however your c1() function is actually with a capital C (C1()), so that'll error out there.
2. Your running the menu inside the password lock, so when you do something with the keyboard (line 81) it goes back to the start of the loop and does it all again. You need to exit out of that loop and start another loop to deal with the arrow keys and enter button. Not too hard. Here's an example of exiting a loop:

while true do
  print("Breaking..")
  break
end
print("Broke it!")
--# OR the more recommended way
local bRunning = true
while bRunning do
  print("Breaking..")
  bRunning = false
end
print("Broke it!")

Hope this helps.
Luca_S #3
Posted 01 November 2017 - 09:31 AM
Another solution would be this:

if input=="Password"then
  term.clear()
  term.setCursorPos(1,1)
  print("-=Welcome Authorized Personnel=-")
  term.setCursorPos(1,2)
  running = true
  while running do
    printMenu(mainMenu)
    event, key = os.pullEvent("key")
    onKeyPressed(key,mainMenu)
  end
else
And then have Exit() set bRunning to false.
Here is an updated version of your program: bV3B6XrS
GamersUnited556 #4
Posted 01 November 2017 - 11:25 PM
Tank you for the replys, I will try them and get back, also with the function for c1 was just when i re-typed it on my pc
GamersUnited556 #5
Posted 01 November 2017 - 11:41 PM
Davee-ee Jones, Where would I implement that?
Dave-ee Jones #6
Posted 02 November 2017 - 12:32 AM
Davee-ee Jones, Where would I implement that?

I was giving examples of effectively exitting a loop. It looks like (based on your code) that is has already been implemented, though. Good luck! :)/>
GamersUnited556 #7
Posted 02 November 2017 - 01:59 AM
Another solution would be this:

if input=="Password"then
  term.clear()
  term.setCursorPos(1,1)
  print("-=Welcome Authorized Personnel=-")
  term.setCursorPos(1,2)
  running = true
  while running do
	printMenu(mainMenu)
	event, key = os.pullEvent("key")
	onKeyPressed(key,mainMenu)
  end
else
And then have Exit() set bRunning to false.
Here is an updated version of your program: bV3B6XrS

I tried this but it still has the error when it comes to pressing buttons any ideas?
Luca_S #8
Posted 02 November 2017 - 02:44 PM
Another solution would be this:

if input=="Password"then
  term.clear()
  term.setCursorPos(1,1)
  print("-=Welcome Authorized Personnel=-")
  term.setCursorPos(1,2)
  running = true
  while running do
	printMenu(mainMenu)
	event, key = os.pullEvent("key")
	onKeyPressed(key,mainMenu)
  end
else
And then have Exit() set bRunning to false.
Here is an updated version of your program: bV3B6XrS

I tried this but it still has the error when it comes to pressing buttons any ideas?
That's strange it didn't error for me, but I checked in a newer CC Version. Going to check in the specified version when I'm home.

Why are you using an outdated CC/MC Version tho?
GamersUnited556 #9
Posted 02 November 2017 - 11:53 PM
Another solution would be this:

if input=="Password"then
  term.clear()
  term.setCursorPos(1,1)
  print("-=Welcome Authorized Personnel=-")
  term.setCursorPos(1,2)
  running = true
  while running do
	printMenu(mainMenu)
	event, key = os.pullEvent("key")
	onKeyPressed(key,mainMenu)
  end
else
And then have Exit() set bRunning to false.
Here is an updated version of your program: bV3B6XrS

I tried this but it still has the error when it comes to pressing buttons any ideas?
That's strange it didn't error for me, but I checked in a newer CC Version. Going to check in the specified version when I'm home.

Why are you using an outdated CC/MC Version tho?

In an older modpack (Tekkit Classic) I like to go back and play on the version I started with
Edited on 02 November 2017 - 10:53 PM
valithor #10
Posted 03 November 2017 - 05:25 AM
If I am remembering correctly. The version of CC you are using does not have the keys api, so you can not use keys.enter on line 48 (or keys.anything for that matter).

You can either port the keys api to your version (go and grab the api from the current version and just putting it on your computer and calling os.loadAPI on the file), or you can figure out what the key code for enter is.

To figure out what the key code for enter is go to the lua prompt. Type print(textutils.serialize({os.pullEvent("key")})) and then hit the enter key. You should see a serialized table containing the word "key" and the key code the corresponds to the enter key.
Luca_S #11
Posted 03 November 2017 - 01:39 PM
If I am remembering correctly. The version of CC you are using does not have the keys api, so you can not use keys.enter on line 48 (or keys.anything for that matter).

You can either port the keys api to your version (go and grab the api from the current version and just putting it on your computer and calling os.loadAPI on the file), or you can figure out what the key code for enter is.

To figure out what the key code for enter is go to the lua prompt. Type print(textutils.serialize({os.pullEvent("key")})) and then hit the enter key. You should see a serialized table containing the word "key" and the key code the corresponds to the enter key.

According to the wiki the keys api has been added in CC 1.4
valithor #12
Posted 03 November 2017 - 08:00 PM
If I am remembering correctly. The version of CC you are using does not have the keys api, so you can not use keys.enter on line 48 (or keys.anything for that matter).

You can either port the keys api to your version (go and grab the api from the current version and just putting it on your computer and calling os.loadAPI on the file), or you can figure out what the key code for enter is.

To figure out what the key code for enter is go to the lua prompt. Type print(textutils.serialize({os.pullEvent("key")})) and then hit the enter key. You should see a serialized table containing the word "key" and the key code the corresponds to the enter key.

According to the wiki the keys api has been added in CC 1.4

If he is in fact using tekkit classic (as he said in a later reply), then he is using CC version 1.33 (Don't know why they use the older version). In addition the error is attempt to index nil, which means there is a table he is indexing which is nil. The only table he is indexing on line 48 is the keys table.

tekkit classic mods:
https://www.technicpack.net/modpack/tekkit.552560/mods
Bomb Bloke #13
Posted 03 November 2017 - 08:34 PM
In which case replacing "keys.enter" with "28" should do the trick, per the chart here.
GamersUnited556 #14
Posted 04 November 2017 - 02:20 AM
sweet that worked, thank you all