Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- playerDetectorDoor --
- -- https://pastebin.com/tKfbvZT2 --
- -- CONFIGURATION --
- local detectionRange = 8 -- Detection range in blocks
- local redstoneDuration = 4 -- Duration (in seconds) for redstone signal
- local invertMode = false -- Invert redstone signal: true = always on, off when player detected
- local redstoneSide = "top" -- Side where redstone is emitted
- -- FINDING THE PLAYER DETECTOR AUTOMATICALLY --
- local detector = nil
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "playerDetector" then
- detector = peripheral.wrap(name)
- break
- end
- end
- if not detector then
- print("No player detector found!")
- return
- end
- print("Player detector found on " .. peripheral.getName(detector))
- print("Monitoring for players...")
- -- MAIN LOOP --
- while true do
- local players = detector.getPlayersInRange(detectionRange)
- local playerDetected = #players > 0
- if invertMode then
- redstone.setOutput(redstoneSide, not playerDetected)
- else
- redstone.setOutput(redstoneSide, playerDetected)
- end
- if playerDetected then
- sleep(redstoneDuration)
- redstone.setOutput(redstoneSide, invertMode) -- Reset output after duration
- end
- sleep(0.01) -- Short delay to prevent excessive CPU usage
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement