15 posts
Location
united kingdom
Posted 19 March 2014 - 04:09 AM
Hi i keep getting this error bios:339: [string "def"]:52: 'then' expected when i try to run my code
its saying im missing a then on line 52 but it has one
any help is greatly appreciated
Mon = peripheral.wrap("top")
local function centerText(text)
x,y = m.getSize()
x1,y1 = m.getCursorPos()
Mon.setCursorPos((math.floor(x/2) - (math.floor(#text/2))), y1)
Mon.write(text)
end
local function defence()
centerText("===============================================")
Mon.setCursorPos(1, 2)
centerText("WARNING!")
Mon.setCursorPos(1, 3)
centerText("INCOMING MISSILE")
Mon.setCursorPos(1, 4)
centerText("ACTIVATING FORCEFIELD")
Mon.setCursorPos(1, 5)
redstone.setOutput("right", true)
minutes = 5
seconds = 0
clocktime = minutes+seconds
repeat
if seconds == 0 then
minutes = minutes-1
seconds = 60
end
if minutes == -1 then
break
end
seconds = seconds-1
if seconds < 10 then
zero = 0
else
zero = ""
end
Mon.setCursorPos(1, 5)
centerText("Forcefield will be active for: "..minutes..":"..zero..seconds)
Mon.setCursorPos(1, 6)
centerText("===============================================")
until minutes == -1
sleep(0.1)
Mon.setCursorPos(1, 5)
Mon.clearLine()
CenterText("FORCEFIELD DEACTIVATED!")
sleep(0.5)
redstone.setOutput("right", false)
end
function check()
if redstone.getInput("left") == true then --THIS IS THE LINE THAT I GET AN ERROR WITH
defence()
end
elseif redstone.getInput("left") == false then
check()
end
7083 posts
Location
Tasmania (AU)
Posted 19 March 2014 - 05:14 AM
The line you've flagged looks valid to me, but a few lines under it I can see you're trying to pull an "elseif" on an "if" block you've already "end"ed.
Just for kicks, try this:
if redstone.getInput("left") then defence() else check() end
Edit:
Well, that'd run directly into another issue - spam of recursive function calls causing a stack overflow and hence script crash - so maybe try this:
local function check()
while true do -- Start an indefinitely repeating loop.
if redstone.getInput("left") then defence() end -- Perform the check.
os.pullEvent("redstone") -- Wait for a redstone state change.
end
end
Edited on 19 March 2014 - 04:19 AM
15 posts
Location
united kingdom
Posted 19 March 2014 - 09:42 AM
ok will give it a try as soon as im home thanks
15 posts
Location
united kingdom
Posted 19 March 2014 - 10:07 AM
ok i have tried it but it still gives the exact same error
heres the code
Mon = peripheral.wrap("top")
local function centerText(text)
x,y = m.getSize()
x1,y1 = m.getCursorPos()
Mon.setCursorPos((math.floor(x/2) - (math.floor(#text/2))), y1)
Mon.write(text)
end
local function defence()
centerText("===============================================")
Mon.setCursorPos(1, 2)
centerText("WARNING!")
Mon.setCursorPos(1, 3)
centerText("INCOMING MISSILE")
Mon.setCursorPos(1, 4)
centerText("ACTIVATING FORCEFIELD")
Mon.setCursorPos(1, 5)
redstone.setOutput("right", true) --Change "right" to the side thats will activate the forcefield
minutes = 5
seconds = 0
clocktime = minutes+seconds
repeat
if seconds == 0 then
minutes = minutes-1
seconds = 60
end
if minutes == -1 then
break
end
seconds = seconds-1
if seconds < 10 then
zero = 0
else
zero = ""
end
Mon.setCursorPos(1, 5)
centerText("Forcefield will be active for: "..minutes..":"..zero..seconds)
Mon.setCursorPos(1, 6)
centerText("===============================================")
until minutes == -1
sleep(0.1)
Mon.setCursorPos(1, 5)
Mon.clearLine()
CenterText("FORCEFIELD DEACTIVATED!")
sleep(0.5)
redstone.setOutput("right", false) --Change "right" to the side thats will activate the forcefield
end
local function check()
while true do-- Start an indefinitely repeating loop.
if redstone.getInput("left") then defence() end -- Perform the check.
os.pullEvent("redstone") -- Wait for a redstone state change.
end
end
7508 posts
Location
Australia
Posted 19 March 2014 - 10:29 AM
ok i have tried it but it still gives the exact same error
-code snip-
are you sure? doesn't error for me.
15 posts
Location
united kingdom
Posted 20 March 2014 - 12:32 AM
Thanks guys i got it working
47 posts
Posted 20 March 2014 - 04:38 PM
I don't know if anyone already caught this, and it doesn't matter since fixing the original code doesn't really fix it as you then have to deal with a stack overflow, but the reason the original code didn't work right was because of where you placed your end's:
function check()
if redstone.getInput("left") == true then
defence()
end--this shouldn't be here, an if statement only needs one end, which should come after any elseif or else statements, this was what was causing your weird compiler error
elseif redstone.getInput("left") == false then
check()
end--this should be the only end for the entire if statement
--but here is another issue, there is no end for the "check" function, you would get a compiler error here as well
it should read:
function check()
if redstone.getInput("left") == true then
defence()
elseif redstone.getInput("left") == false then
check()--this causes a stack overflow eventually, the code will compile and run, but it will crash at runtime
end
end
8543 posts
Posted 20 March 2014 - 05:30 PM
Bomb Bloke's initial reply did mention the issue with the end before the elseif.