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

[Lua][Error]open:19: attempt to call nil

Started by LemonyLime, 08 April 2013 - 08:24 AM
LemonyLime #1
Posted 08 April 2013 - 10:24 AM
state = 0
active = 1

while ( active == 1 ) do
  if ( state == 1 ) then
    io.write( "Door is Open.\n" )
    else
	  io.write( "Door is Closed.\n" )
  end
 
  io.write( "Enter command(-1 to exit): " )

  state = read()
 
  if ( state == -1 ) then
    active = 0
    else
      redstone.setoutput( "right", state ~= 0 )
  end

end


This is just a simple program that sends a redstone signal to open a piston door. 0 = door shut, -1 = close program, anything else = door open.
it doesn't think that redstone.setoutput() is an actual function… I checked the apis folder of the computer, and redstone wasn't there, but the default program redpulse works, using the exact same function I am. What's going on?
SadKingBilly #2
Posted 08 April 2013 - 10:26 AM
"redstone.setOutput()"?

Lua does not differentiate between equal and identical like PHP and JavaScript, so doing "string == int" will always return false. And Lua does not enclose conditions in brackets. Anyway, here is the fixed code:

state = 0
active = 1
while active == 1 do
	if state == 1 then
		io.write( "Door is Open.\n" )
	else
		io.write( "Door is Closed.\n" )
	end
	io.write( "Enter command(-1 to exit): " )
	state = read()
	if state == "-1" then
		active = 0
	else
		redstone.setOutput("right", state ~= "0")
	end
end
PixelToast #3
Posted 08 April 2013 - 10:26 AM
its rs.setOutput also
Kilobyte #4
Posted 08 April 2013 - 11:42 AM
And Lua does not enclose conditions in brackets.

Yes, however its still valid syntax. (at least for if or while)
QuantumGrav #5
Posted 08 April 2013 - 12:26 PM
I think you're problem is with putting 'state ~= 0' in your redstone.setOutput call. I don't think that should work, but what would work and still do what I think you're trying to to do would be:

if ( state == -1 ) then
    active = 0
    elseif state ~= 0 then
	  rs.setOutput( "right", state )
  end
I might be misunderstanding what you want your code to do, so this may or may not suit you.
I would also try capitalizing the O in redstone.setoutput. That might also fix it.

Try those things and it should work!
PixelToast #6
Posted 08 April 2013 - 12:29 PM
I think you're problem is with putting 'state ~= 0' in your redstone.setOutput call. I don't think that should work, but what would work and still do what I think you're trying to to do would be:

if ( state == -1 ) then
	active = 0
	elseif state ~= 0 then
	  rs.setOutput( "right", state )
  end
I might be misunderstanding what you want your code to do, so this may or may not suit you.
I would also try capitalizing the O in redstone.setoutput. That might also fix it.

Try those things and it should work!
he used strings for numbers, this wont work unless you quote the numbers
and why are you setting it to state?
anything nonfalse and nonnil will be true .-.
SadKingBilly #7
Posted 08 April 2013 - 12:35 PM
I think you're problem is with putting 'state ~= 0' in your redstone.setOutput call. I don't think that should work, but what would work and still do what I think you're trying to to do would be:

if ( state == -1 ) then
	active = 0
	elseif state ~= 0 then
	  rs.setOutput( "right", state )
  end
I might be misunderstanding what you want your code to do, so this may or may not suit you.
I would also try capitalizing the O in redstone.setoutput. That might also fix it.

Try those things and it should work!
he used strings for numbers, this wont work unless you quote the numbers
and why are you setting it to state?
anything nonfalse and nonnil will be true .-.
He probably just doesn't realize that the second argument of redstone.setOutput() is a bool. He'd be entirely right if the second argument were a string.
LemonyLime #8
Posted 08 April 2013 - 04:55 PM
I had a little more time with it, and settled on this as the final product. Works as intended. Thanks for the help.


state = "close"
active = true

while active do
	if state == "open" then
		io.write( "Door is Open.\n" )
		else
			io.write( "Door is Closed.\n" )
	end
	
	io.write( "Enter command(open, close, exit): " )
	state = read()
	
	if state == "exit" then
		 active = false
		 state = "close"
	end
	redstone.setOutput("right", state == "open")
end



The thing that was actually giving me that error was not capitalizing the O in output, but I had plenty of other things to fix after that.

Lua does not differentiate between equal and identical like PHP and JavaScript, so doing "string == int" will always return false.
I thought everything was an int, but I'm guessing read only returns a string, even if you enter a number?
QuantumGrav #9
Posted 08 April 2013 - 05:17 PM
Yep! The command read() or io.read() will give you a string no matter what is typed into it. The command 'tonumber(string)' is really helpful for this. If you use 'tonumber(read())' that will turn whatever is inputted into a number as soon as you get it. Pretty useful for a lot of things!