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

[Help][Lua] I don't understand why my code doesn't work

Started by GoldenJz, 26 August 2012 - 07:50 AM
GoldenJz #1
Posted 26 August 2012 - 09:50 AM
Here is my code:

input=redstone.getInput("back")

if input =="false" then
write="Do you want to turn lights on?"
local YesOrNo = read()
if YesOrNo =="yes" then
rs.setOutput("back", true)
end
if YesOrNo =="no" then
end

elseif input =="true" then
write="Do you want to turn lights off?
local YesOrNo2 = read()
if YesOrNo2 =="yes" then
rs.setOutput("back", false)
if YesOrNo2 =="no" then
end
end
end

when i run the program it doesn't write anything any help?
It's to control a light switch
sjele #2
Posted 26 August 2012 - 10:21 AM

input=redstone.getInput("back")
if not input  then
write("Do you want to turn lights on?")
local YesOrNo = string.lower(read()) --Makes it so it's allways lower case answer.
if YesOrNo =="yes" then
rs.setOutput("back", true)
else
end
elseif input  then
write("Do you want to turn lights off?") --Write is used like this write("text")
local YesOrNo2 =string,lower(read())
if YesOrNo2 =="yes" then
rs.setOutput("back", false)
else
end
end

You might need to add an end after it. Couldent realy see if i need one or not.
Teraminer #3
Posted 26 August 2012 - 06:22 PM
I think you misted some spaces
Grim Reaper #4
Posted 26 August 2012 - 07:26 PM

input=redstone.getInput("back")
if input =="false" then
write="Do you want to turn lights on?"
local YesOrNo = read()
if YesOrNo =="yes" then
rs.setOutput("back", true)
end
if YesOrNo =="no" then
end
elseif input =="true" then
write="Do you want to turn lights off?
local YesOrNo2 = read()
if YesOrNo2 =="yes" then
rs.setOutput("back", false)
if YesOrNo2 =="no" then
end
end
end

The first thing that I noticed that you should take into account is that rs.getInput() only checks for input during that current cycle, so if there is no input at the exact moment that instruction is executed then it will yield false.

Another thing that I noticed is that you're checking for strings not boolean values.
Try what sjele suggested:

if not input then
-- ...
else
-- ...
end

You're also assigning write as an identifier for the string "Do you want the lights on?" instead of calling the write method.

write( "Do you want the lights on?" )

sjele's code should work just fine, but here is my version:


input = redstone.getInput("back")
local YesOrNo = "" -- Initialize YesOrNo to an empty string.

if not input then -- If input is equal to false
   write("Do you want to turn lights on?")
   YesOrNo = string.lower( read() ) -- Force the string entered to lower case.

   if YesOrNo == "yes" then -- If YesOrNo is equal to "yes".
      rs.setOutput("back", true) -- Set the back redstone output to true.
   elseif YesOrNo == "no" then -- If YesOrNo is not equal to "yes" but is equal to "no".
      rs.setOutput("back", false) -- Set the back redstone output to false.
   end
else -- If input is anything but false.
   write("Do you want to turn lights off?")
   YesOrNo = string.lower( read() ) -- Force the string entered to lower case.

   if YesOrNo == "yes" then -- If YesOrNo is equal to "yes".
      rs.setOutput("back", false) -- Set the back redstone output to false.
   end
end