I'm gonna copy where your program goes wrong, to why it goes wrong.
function Choice2 ()
inLightsMenu = true
selectedItem = 1
while inLightsMenu do
term.clear ()
term.setCursorPos (1, 1)
printMenu (lightsMenu) --#Line 34
event, key = os.pullEvent ("key")
onKeyPressed (key, lightsMenu)
end
end
function LightsOn ()
inLightsMenu = false
end
function LightsOff ()
inLightsMenu = false
end
function Exit ()
inMainMenu = false
end
---------------
-- Main Menu --
---------------
local mainMenu = {
[1] = {text = "Choice 1", handler = Choice1},
[2] = {text = "Choice 2", handler = Choice2},
[3] = {text = "Exit" , handler = Exit},
}
local lightsMenu = {
[1] = {text = "Lights On" , handler = LightsOn},
[2] = {text = "Lights Off", handler = LightsOff},
}
Your problem here, is that at line 34 you're calling the function printMenu and passing the lightsMenu table to it. However you're not defining lightsMenu until later.
The Lua interpreter sees that lightsMenu isn't defined at the moment it reads that function and then says it's going to have to look for the global lightMenu when i get there, however you are declaring lightsMenu as local, so it's never going to be the global lightsMenu. So you're passing nil the printMenu and it's erroring because it cannot get the length of a nil value.
An easy fix for this is to move your lightsMenu and mainMenu tables to the top of your code, so that every function in the program has access to it.