Advertisement
DOGGYWOOF

Messaging test (May not work)

Jun 8th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. -- Initialize rednet
  2. local modemSide = "right" -- Adjust side based on where your modem is attached
  3. rednet.open(modemSide)
  4.  
  5. print("Enter the ID of the computer you want to message:")
  6. local targetID = tonumber(read())
  7.  
  8. if not targetID then
  9. print("Invalid computer ID.")
  10. return
  11. end
  12.  
  13. print("Connected to computer " .. targetID)
  14.  
  15. -- Function to send messages
  16. local function sendMessage(targetID)
  17. while true do
  18. print("Type your message: ")
  19. local message = read()
  20. rednet.send(targetID, message)
  21. print("Message sent to " .. targetID)
  22. end
  23. end
  24.  
  25. -- Function to receive messages
  26. local function receiveMessage()
  27. while true do
  28. local senderID, message, protocol = rednet.receive()
  29. if senderID == targetID then
  30. print("Message from " .. senderID .. ": " .. message)
  31. end
  32. end
  33. end
  34.  
  35. -- Run sendMessage and receiveMessage functions simultaneously
  36. parallel.waitForAny(sendMessage, receiveMessage, targetID)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement