2

Hi I want my lua code in Computercraft to allow the user to turn the redstone signal on/off by right clicking on a monitor on top, but I can't get it to work.

monitor = peripheral.wrap("top")
monitor.clear()
monitor.setTextColor(colors.red)
monitor.setCursorPos(1, 1)
monitor.setTextScale(1)
monitor.write("Hello")

function rubber()
    monitor.setCursorPos(1, 2)
    monitor.clearLine()

    if rs.getOutput("right", true) then
        monitor.write("Rubber farm is on")
    elseif rs.getOutput("right", false) then
        monitor.write("Rubber farm is off")
    end

    local event = { os.pullEvent() }

    if event == "monitor_touch" then
        if rs.getOutput("right") == true then
            rs.setOutput("right", false)
        else
            rs.setOutput("right", true)
        end
    else
        write("test")
    end

    rubber()
end

Right now all it displays is 'hello' and I don't know how to fix it, anyone know how? Also I'm a beginner at Lua so I've probably made some pretty simple mistakes. Thanks

Kamiccolo
  • 7,758
  • 3
  • 34
  • 47
user3412375
  • 51
  • 1
  • 1
  • 3

6 Answers6

2
local event = { os.pullEvent() }
if event == "monitor_touch" then

os.pullEvent returns a tuple. In your code, you're packing this tuple into a table. That's fine, but you then compare that table to a string. Tables can't be equal to strings - they're a table. Either don't pack the tuple into a table, and keep the first return value (the type):

local event = os.pullEvent()
if event == "monitor_touch" then

Or extract the first element when comparing

local event = { os.pullEvent() }
if event[1] == "monitor_touch" then
Eric
  • 95,302
  • 53
  • 242
  • 374
1

The problem is you wanted to have that function infinitly looping, but you have not called your function outside your function.... also you should look into using while loops

while true do
 //stuff here
end

just add

rubber()

to the last line after your last end tag.

Alan Doyle
  • 98
  • 16
0

You have to call the function. rubber()

Albin9000
  • 343
  • 1
  • 2
  • 4
0

You need to close your function

function rubber()

  monitor.setCursorPos(1,1)

  monitor.clearLine()
end

The end is it you need to make this little word

trizz
  • 1,447
  • 12
  • 27
0

this is a simple fix, simply add rubber() after you finish the function rubber, cause while you have created the function rubber, you have not called for it to start yet.

Edward Ford
  • 71
  • 1
  • 1
  • 3
0

The "monitor_touch" event is what you should be using. Also, make sure the monitor you are using is an advanced monitor (the one with the yellow border).

If you need help in understanding the event, check out this page: http://computercraft.info/wiki/Monitor_touch_(event)

Kevin Williams
  • 186
  • 1
  • 16