Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- player_detectorDoor --
- -- CONFIGURATION --
- local detectionRange = 8 -- Detection range in blocks
- local redstoneDuration = 4 -- Delay (in seconds) before turning off after player leaves
- local invertMode = false -- Invert signal logic
- 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) == "player_detector" 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...")
- -- TRACKING STATE --
- local playerPresent = false
- local lastPlayerTime = os.clock()
- local redstoneState = false
- -- FUNCTION TO SET REDSTONE CLEANLY
- local function setRedstone(state)
- if redstoneState ~= state then
- redstone.setOutput(redstoneSide, state)
- redstoneState = state
- print("Redstone " .. (state and "ON" or "OFF"))
- end
- end
- -- MAIN LOOP --
- while true do
- local players = detector.getPlayersInRange(detectionRange)
- local detected = #players > 0
- if detected then
- lastPlayerTime = os.clock()
- playerPresent = true
- else
- playerPresent = false
- end
- local elapsed = os.clock() - lastPlayerTime
- local shouldBeOn
- if invertMode then
- shouldBeOn = not playerPresent and elapsed < redstoneDuration
- else
- shouldBeOn = playerPresent or (elapsed < redstoneDuration)
- end
- setRedstone(shouldBeOn)
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement