Advertisement
MajorVictory

Door detector

May 20th, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.97 KB | None | 0 0
  1. local sens = peripheral.wrap("left")
  2. local outside = "top"
  3. local door = false
  4. local radius = 5
  5.  
  6. door = false
  7. redstone.setOutput(outside, (not door))
  8.  
  9. function getPos(username)
  10.     local ok, player = pcall(sens.getPlayerByName, username)
  11.     if ok then
  12.         return player.select("living").position
  13.     end
  14. end
  15.  
  16. function chkDoor()
  17.   redstone.setOutput(outside, (not door))
  18. end
  19.  
  20. function chkPlayer()
  21.     local players = sens.getPlayers()
  22.  
  23.     if #players ~= 0 then
  24.         for k=1,#players do
  25.  
  26.             local pos = getPos(players[k].name)
  27.             if pos and pos.x >= (radius * -1) and pos.x <= radius
  28.                 and pos.y >= (radius * -1) and pos.y <= radius
  29.                 and pos.z >= (radius * -1) and pos.z <= radius then
  30.     --          print(username.."is in the spot. Opening Door.")
  31.                 door = true
  32.                 return
  33.             end
  34.         end
  35.     end
  36.  
  37.     door = false
  38. end
  39.  
  40. while true do
  41.     sleep(0.5)
  42.     chkDoor()
  43.     chkPlayer()
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement