Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CONFIGURATION --
- local detectionRange = 2 -- Detection range in blocks
- local redstoneDuration = 3 -- Duration (in seconds) for redstone signal
- local invertMode = true -- Invert redstone signal: true = always on, off when player detected
- local redstoneSide = "bottom" -- 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.1) -- Short delay to prevent excessive CPU usage
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement