Advertisement
Randomaytion13

Turtle connection

May 22nd, 2024
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.69 KB | None | 0 0
  1. local http = require("http")
  2.  
  3. local function sendToServer(data)
  4.     local response = http.post("http://localhost:3000/turtle", textutils.serialize(data))
  5.     if response then
  6.         print(response.readAll())
  7.     else
  8.         print("Failed to connect to server")
  9.     end
  10. end
  11.  
  12. while true do
  13.     local command = http.get("http://localhost:3000/command")
  14.     if command then
  15.         local cmd = command.readAll()
  16.         if cmd == "forward" then
  17.             turtle.forward()
  18.         elseif cmd == "back" then
  19.             turtle.back()
  20.         end
  21.         sendToServer({x = turtle.getPosition().x, y = turtle.getPosition().y, z = turtle.getPosition().z})
  22.     end
  23.     os.sleep(1)
  24. end
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement