Advertisement
BigBlow_

playerDetectorDoor_v2

Jun 15th, 2025 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- player_detectorDoor --
  2. -- CONFIGURATION --
  3. local detectionRange = 8 -- Detection range in blocks
  4. local redstoneDuration = 4 -- Delay (in seconds) before turning off after player leaves
  5. local invertMode = false -- Invert signal logic
  6. local redstoneSide = "top" -- Side where redstone is emitted
  7.  
  8. -- FINDING THE PLAYER DETECTOR AUTOMATICALLY --
  9. local detector = nil
  10. for _, name in ipairs(peripheral.getNames()) do
  11. if peripheral.getType(name) == "player_detector" then
  12. detector = peripheral.wrap(name)
  13. break
  14. end
  15. end
  16.  
  17. if not detector then
  18. print("No player detector found!")
  19. return
  20. end
  21.  
  22. print("Player detector found on " .. peripheral.getName(detector))
  23. print("Monitoring for players...")
  24.  
  25. -- TRACKING STATE --
  26. local playerPresent = false
  27. local lastPlayerTime = os.clock()
  28. local redstoneState = false
  29.  
  30. -- FUNCTION TO SET REDSTONE CLEANLY
  31. local function setRedstone(state)
  32. if redstoneState ~= state then
  33. redstone.setOutput(redstoneSide, state)
  34. redstoneState = state
  35. print("Redstone " .. (state and "ON" or "OFF"))
  36. end
  37. end
  38.  
  39. -- MAIN LOOP --
  40. while true do
  41. local players = detector.getPlayersInRange(detectionRange)
  42. local detected = #players > 0
  43.  
  44. if detected then
  45. lastPlayerTime = os.clock()
  46. playerPresent = true
  47. else
  48. playerPresent = false
  49. end
  50.  
  51. local elapsed = os.clock() - lastPlayerTime
  52.  
  53. local shouldBeOn
  54. if invertMode then
  55. shouldBeOn = not playerPresent and elapsed < redstoneDuration
  56. else
  57. shouldBeOn = playerPresent or (elapsed < redstoneDuration)
  58. end
  59.  
  60. setRedstone(shouldBeOn)
  61.  
  62. sleep(0.1)
  63. end
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement