Advertisement
BigBlow_

playerDetectorDoor

Mar 16th, 2025 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. -- CONFIGURATION --
  2. local detectionRange = 2 -- Detection range in blocks
  3. local redstoneDuration = 3 -- Duration (in seconds) for redstone signal
  4. local invertMode = true -- Invert redstone signal: true = always on, off when player detected
  5. local redstoneSide = "bottom" -- Side where redstone is emitted
  6.  
  7. -- FINDING THE PLAYER DETECTOR AUTOMATICALLY --
  8. local detector = nil
  9. for _, name in ipairs(peripheral.getNames()) do
  10. if peripheral.getType(name) == "playerDetector" then
  11. detector = peripheral.wrap(name)
  12. break
  13. end
  14. end
  15.  
  16. if not detector then
  17. print("No player detector found!")
  18. return
  19. end
  20.  
  21. print("Player detector found on " .. peripheral.getName(detector))
  22. print("Monitoring for players...")
  23.  
  24. -- MAIN LOOP --
  25. while true do
  26. local players = detector.getPlayersInRange(detectionRange)
  27. local playerDetected = #players > 0
  28.  
  29. if invertMode then
  30. redstone.setOutput(redstoneSide, not playerDetected)
  31. else
  32. redstone.setOutput(redstoneSide, playerDetected)
  33. end
  34.  
  35. if playerDetected then
  36. sleep(redstoneDuration)
  37. redstone.setOutput(redstoneSide, invertMode) -- Reset output after duration
  38. end
  39.  
  40. sleep(0.1) -- Short delay to prevent excessive CPU usage
  41. end
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement