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

[lua] problem with the if command

Started by PixelToast, 10 May 2012 - 02:05 AM
PixelToast #1
Posted 10 May 2012 - 04:05 AM
well i made a remote command thing



shell.run("clear")
print("computer id is: "..os.getComputerID())
write("modem direction: ")
rednet.open(read())
write("controller id: ")
controller=read()
while true do
  id,data=rednet.receive()
  print("received '"..data.."' from id: '"..id.."' controller id: '"..controller.."'")
  if id == controller then
    print("executing")
    local tWords = {}
    for match in string.gmatch(data, "[^ \t]+") do
      table.insert( tWords, match )
    end
    local sCommand = tWords[1]
    if sCommand then
      shell.run( sCommand, unpack( tWords, 2 ) )
    end
print("done")
  end
end

but for some reason id == controller wont return true when it displays "received 'something' from id: '0' controller id: '0'"

:C
Xtansia #2
Posted 10 May 2012 - 04:37 AM
Because id returned by rednet.receive() is a number while controller returned by read() is a string, This is easily fixed by changing:
controller = read()
To:
controller = tonumber(read())
PixelToast #3
Posted 10 May 2012 - 06:17 PM
oh god thank you
i just learned the basics of lua and this will help me alot