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

Help with monitor cloning

Started by ellercom, 01 December 2012 - 07:45 PM
ellercom #1
Posted 01 December 2012 - 08:45 PM
i have this code here and i get the following error

tomon:5: attempt to call nil

here is my code

sides = {"left","right","front","back","top","bottom"}
for i, v in ipairs(sides) do
if peripheral.isPresent(v) then
if peripheral.getType(v) == "monitor" then
term.redirect(peripheral.wrap(v))
break;
end
end
end

i am completely new at computercraft and i cant get ahold of the original creator of the code and i cant get it to work so please help.
Luanub #2
Posted 01 December 2012 - 09:25 PM
Try removing the ; after the break you don't need it in Lua.

Its probably better to use pairs instead of ipairs as well. ipairs is being depreciated in future releases of Lua.

You can also make a couple of minor improvements.


for _,v in pairs(rs.getSides()) do
if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
  term.redirect(peripheral.wrap(v))
  break
end
end
ellercom #3
Posted 02 December 2012 - 06:42 AM
i tried you code that you suggested and i am still getting a attempt to call nil error on line 3
ChunLing #4
Posted 02 December 2012 - 07:42 AM
insert a debug print, like "print(type(v),v,type(term),type(term.redirect),type(peripheral),type(peripheral.wrap))" Cause something is wonky.
ellercom #5
Posted 02 December 2012 - 07:52 AM
it says

bios:206: [string "tomon"]:5: 'end' expected (to close 'if' and line 2)

here is the current code

for _,v in pairs(rs.getSides()) do
if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
term.redirect(peripheral.wrap(v))
break

print(type(v),v,type(term),type(term.redirect),type(peripheral),type(peripheral.wrap))end
end
anonimo182 #6
Posted 02 December 2012 - 08:25 AM
it says

bios:206: [string "tomon"]:5: 'end' expected (to close 'if' and line 2)

here is the current code

for _,v in pairs(rs.getSides()) do
if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
term.redirect(peripheral.wrap(v))
break

print(type(v),v,type(term),type(term.redirect),type(peripheral),type(peripheral.wrap))end
end
You forgot an end at the end
ellercom #7
Posted 02 December 2012 - 08:47 AM
it says

bios:206: [string "tomon"]:5: 'end' expected (to close 'if' and line 2)

here is the current code

for _,v in pairs(rs.getSides()) do
if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
term.redirect(peripheral.wrap(v))
break

print(type(v),v,type(term),type(term.redirect),type(peripheral),type(peripheral.wrap))end
end
You forgot an end at the end
its there it just didnt transfer on here correctly when i copied and pasted
Luanub #8
Posted 02 December 2012 - 10:03 AM
Make sure to put the break right before the end. Move the debug print up a line or down a line. But the code really should work make sure it matches exactly what I put. It works when I run, I get no errors.
ChunLing #9
Posted 03 December 2012 - 03:07 AM
The debug line has to go before you actually try to use those values for anything else. So:
for _,v in pairs(rs.getSides()) do
  if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
print(type(v),v,type(term),type(term.redirect),type(peripheral),type(peripheral.wrap))
    term.redirect(peripheral.wrap(v))
    break
  end
end
But given that you put the debug line where you did, I think that the most likely wonky thing is that you didn't make the code exactly match what luanub posted. Because that definitely should have worked, the debug line was to find out how it possibly could have failed.