4 posts
Posted 29 June 2013 - 12:59 AM
i keep getting this error with this basic program i wrote to lock doors. its supposed to ask the user what direction he wants the redstone to be outputted, how long before it closes,a password and what word they want to end the program then go to a enter password menu on a loop until the the word typed to close the program is entered. iv only been learning lua for a day now so please try to explain everything to me so i know what i'm doing wrong. thanks in advance!!
http://pastebin.com/z5KHCpTP
8543 posts
Posted 29 June 2013 - 02:24 PM
Split into new topic.
You can't use just numbers as names for variables. Change your function declarations to use actual names instead.
4 posts
Posted 29 June 2013 - 03:34 PM
thanks that worked didn't know i couldn't use numbers for that but now i'm getting "}" expected to close "{" at line 28 and not sure why.
1522 posts
Location
The Netherlands
Posted 29 June 2013 - 03:54 PM
Spoiler
--variables/functions--
--------------------------------
local termWidth, termHeight = term.getSize()
function c1()
z = "front"
end
function c2()
z = "back"
end
function c3()
z = "left"
end
function c4()
z = "right"
end
function c5()
z = "top"
end
function c6()
z = "bottom"
end
---------------------------------
--redstone config--
---------------------------------
term.clear()
local selectedItem = 1
term.setCursorPos(1,1)
mainMenu = {
text = "what side will the redstone output?", -- You forgot a comma
[1] = { text = "front", handler = c1 },
[2] = { text = "back", handler = c2 },
[3] = { text = "left", handler = c3 },
[4] = { text = "right", handler = c4 },
[5] = { text = "top", handler = c5 },
[6] = { text = "bottom", handler = c6 }
}
function printMenu( menu )
for i=1,#menu do
if i == selectedItem then
print(">> "..menu[i].text)
else
print(" "..menu[i].text)
end
end
end -- You didnt have this end
function onKeyPressed(key, menu)
if key == keys.enter then
onItemSelected(menu)
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem -1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem +1
end
end
end -- You didnt have this end
function onItemSelected( menu )
menu[selectedItem].handler()
end
function main()
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
--end --Doesnt end anything
main()
--end --Doesnt end anything
-----------------------------
--wait time--
------------------------------
print ("how long do you want to wait before the door closes again")
--local a = read()
-- Replaced with this code to make it a number and it only continues if it is a number
local a = nil
repeat
a = tonumber(read())
until a
-------------------------------
--pass select--
--------------------------------
term.clear()
print ("enter program exit word")
local y = read( "*" )
--end --Doesnt end anything
-----------------------------------
--exit select--
-----------------------------------
term.clear()
print ("enter program exit word")
local x = read( "*" )
--end --Doesnt end anything
----------------------------------
--password part--
----------------------------------
term.clear()
term.setCursorPos(1,1)
repeat
print ("input password")
input = read()
if input == y then
redstone.setOutput(z, true)
sleep(a)
redstone.setOutput(z, false)
end
else
print ("sorry try again")
end -- forgot end
until input == x
Rewrote your code in your way, I'd this otherwise. I have put comments on what I did change or correct.
I never have run the code, so I dont know if it will still error
4 posts
Posted 29 June 2013 - 06:12 PM
thanks that helped a lot added a few to many ends though. i'm not getting an error anymore but its just not running now. i type in the program name for it to load and nothing happens. any ideas why?
1522 posts
Location
The Netherlands
Posted 29 June 2013 - 07:21 PM
Spoiler
local termWidth, termHeight = term.getSize()
function c1()
z = "front"
end
function c2()
z = "back"
end
function c3()
z = "left"
end
function c4()
z = "right"
end
function c5()
z = "top"
end
function c6()
z = "bottom"
end
---------------------------------
--redstone config--
---------------------------------
term.clear()
local selectedItem = 1
term.setCursorPos(1,1)
mainMenu = {
text = "what side will the redstone output?", -- You forgot a comma
[1] = { text = "front", handler = "front" },
[2] = { text = "back", handler = "back" },
[3] = { text = "left", handler = "left" },
[4] = { text = "right", handler = "right" },
[5] = { text = "top", handler = "top" },
[6] = { text = "bottom", handler = "bottom" }
}
function printMenu( menu )
for i=1,#menu do
if i == selectedItem then
print(">> "..menu[i].text)
else
print(" "..menu[i].text)
end
end
end -- You didnt have this end
function onKeyPressed(key, menu)
selectedItem = 1
if key == keys.enter then
--
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem -1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem +1
end
end
onItemSelected(menu)
end -- You didnt have this end
function onItemSelected( menu )
_G["z"] = menu[selectedItem].handler
end
function main()
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
--end --Doesnt end anything
main()
--end --Doesnt end anything
-----------------------------
--wait time--
------------------------------
print ("how long do you want to wait before the door closes again")
--local a = read()
-- Replaced with this code to make it a number and it only continues if it is a number
local a = nil
repeat
a = tonumber(read())
until a
-------------------------------
--pass select--
--------------------------------
term.clear()
print ("enter program exit word")
local y = read( "*" )
--end --Doesnt end anything
-----------------------------------
--exit select--
-----------------------------------
term.clear()
print ("enter program exit word")
local x = read( "*" )
--end --Doesnt end anything
----------------------------------
--password part--
----------------------------------
term.clear()
term.setCursorPos(1,1)
repeat
print ("input password")
input = read()
if input == y then
redstone.setOutput(z, true)
sleep(a)
redstone.setOutput(z, false)
else
print ("sorry try again")
end
until input == x
--# Bad indentation
Fixed the code up for you, but this isnt going to work for your desires. Please rewrite this or start with something simpler
4 posts
Posted 29 June 2013 - 08:10 PM
ok thanks for all the help!!