Minecraft Computercraft Rednet.broadcast() isn't passing the proper value and I'm too tired to know why, help. plz

Viewed 179

The full code itself is very convoluted, long, and novice written, basically I'm trying to send x and z coordinates via rednet from a computer to a separate receiver turtle.

Receiver

rednet.broadcast("Awaiting Input!")
xAxis = rednet.receive() --Have tried tonumber(rednet.receive()) on both same result
zAxis = rednet.receive()
rednet.broadcast(xAxis) --Both values return 2 regardless of what I enter in the sender.
rednet.broadcast(zAxis)

sender

if(irrelevant == "genericstringhere") then
    print(rednet.recieve()) --Awaiting Input!
    io.write("X Axis: ") --Pizzaz
    message = io.read() --Have tried using terms like X and xAxis.
    rednet.broadcast(message) --Broadcast whatever tf I typed in.
    sleep(0.3)
    io.write("Z Axis: ") --More Pizzaz
    message = io.read() --Have tried using terms like Z or zAxis
    rednet.broadcast(message) --Broadcast whatever tf I typed in. again.
    print(rednet.receive()) --Receive xAxis value from sender.
    print(rednet.receive()) --Receive zAxis value from sender.
end

Both results at the end of execution return 2 instead of the value I input in io.read() I have tried "tonumber()" "tostring()" and every combination of the two and can't seem to get it to work.

1 Answers

Per the documentation for rednet.receive:

Returns number senderID, any message, string protocol

The 2 you're seeing is the sender ID, not the message. Instead of xAxis = rednet.receive(), do senderId, xAxis = rednet.receive(), and similarly for everywhere else you assign its value. (If you were wondering why print showed you numbers before the messages, that's why.)

Related