I forgot the open call :unsure:/>/>, here's the fixed code:
Sender:
local serverID = 27 -- Change this to the server ID
local sSide = "top" -- Change the side
rednet.open(sSide)
write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
rednet.send(serverID, id..": "..msg)
print("Message sent.")
else
print("ID must be a number.")
end
Server:
local sSide = "top" -- Change the side
rednet.open(sSide)
while true do
local evt, id, msg = os.pullEvent("rednet_message")
print("Message from ", id, ": ", msg)
local sId, sMsg = string.match(msg, "(%d+): (.+)")
if sId and sMsg then
local destID = tonumber(sId)
-- do what you need with the message
print("Sending message to ", destID)
rednet.send(destID, sMsg)
end
end
I tested it and works B)/>/>
If you are using modems you can use a function to autodetect where the modem is (so you don't have to change the side in code). This is how I do it:
function OpenModem()
for _, s in ipairs(rs.getSides()) do
if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
rednet.open(s)
return true
end
end
return false, "No modem found"
end