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

Rednet refuses to work (Bug?)

Started by ToxicC00kie1001, 16 May 2018 - 04:04 AM
ToxicC00kie1001 #1
Posted 16 May 2018 - 06:04 AM
Ok so I'm new to using lua and all I'm trying to do is make a simple program that wirelessly enables and disables the lights. The only problem is how stupidly complex rednet is rednet won't work. Every time I tell it to turn on or off, computer 1 shows the appropriate response to my inputs while computer 2 is still waiting for input from computer 1. I tried searching for examples and stuff online and the wiki to no avail so this is my last resort. Am I doing something wrong or have I discovered a bug?

Computer 1's code (Sender):

–Computer id is 1

print("Lamp on or off?")
print("1=On 0=Off")

rednet.open("back") –modem is indeed here and within range

ans = io.read() – ans is what the user requested

if ans == ("1") then
rednet.send(1, "1")
print("Turned it on!")

elseif ans == ("0") then
rednet.send(1, "0")
print("Turned it off!")

else
print("Invalid Input")
end

Computer 2's code (Receiver):

–Computer id is 4

rednet.open("back") –modem is indeed here and within range

ans = rednet.receive()

if ans == ("1") then
redstone.setOutput("left", true) –Lamp is indeed here

elseif ans == ("0") then
redstone.setOutput("left", false) –Lamp is indeed here

else
print("Invalid Input!")
end
Luca_S #2
Posted 16 May 2018 - 06:11 AM
1. Put your code in Codeblocks when posting on the Forum or, if the code is large, publish it on Pastebin
2. Your send program sends the rednet message to the computer it is currently running on and not to the computer that runs the receive script.

EDIT: Also rednet is not really "stupidly complex" I would suggest that you read the documentation
Edited on 16 May 2018 - 04:14 AM
ToxicC00kie1001 #3
Posted 16 May 2018 - 06:14 AM
1. Put your code in Codeblocks when posting on the Forum or, if the code is large, publish it on Pastebin
2. Your send program sends the rednet message to the computer it is currently running on and not to the computer that runs the receive script.

1. Noted
2. How do I correct this?
Luca_S #4
Posted 16 May 2018 - 06:17 AM
1. Put your code in Codeblocks when posting on the Forum or, if the code is large, publish it on Pastebin
2. Your send program sends the rednet message to the computer it is currently running on and not to the computer that runs the receive script.

1. Noted
2. How do I correct this?

Check the documentation for rednet.send it requires you to give the ID of the receiving computer as the first argument. So in both cases where you are using rednet.send replace the 1 with the ID of the receiving computer(4)
Dog #5
Posted 16 May 2018 - 06:20 AM

2. Your send program sends the rednet message to the computer it is currently running on and not to the computer that runs the receive script.

2. How do I correct this?

Change rednet.send(1, "1") and rednet.send(1, "2") to rednet.send(4, "1") and rednet.send(4, "2") respectively. (EDIT: :ph34r:/> 'd)

Also, you're missing a variable for rednet.receive() and only capturing the sender ID - rednet.receive() returns the sender id and the message.

id, ans = rednet.receive()
Edited on 16 May 2018 - 04:23 AM
ToxicC00kie1001 #6
Posted 16 May 2018 - 06:23 AM
1. Put your code in Codeblocks when posting on the Forum or, if the code is large, publish it on Pastebin
2. Your send program sends the rednet message to the computer it is currently running on and not to the computer that runs the receive script.

1. Noted
2. How do I correct this?

Check the documentation for rednet.send it requires you to give the ID of the receiving computer as the first argument. So in both cases where you are using rednet.send replace the 1 with the ID of the receiving computer(4)

Worked perfectly! Many thanks. :lol:/>