View difference between Paste ID: tiuEDeza and tKfbvZT2
SHOW: | | - or go back to the newest paste.
1-
-- playerDetectorDoor --
1+
-- player_detectorDoor --
2-
-- https://pastebin.com/tKfbvZT2 --
2+
3
local detectionRange = 8         -- Detection range in blocks
4-
local detectionRange = 8   -- Detection range in blocks
4+
local redstoneDuration = 4       -- Delay (in seconds) before turning off after player leaves
5-
local redstoneDuration = 4  -- Duration (in seconds) for redstone signal
5+
local invertMode = false         -- Invert signal logic
6-
local invertMode = false    -- Invert redstone signal: true = always on, off when player detected
6+
local redstoneSide = "top"       -- Side where redstone is emitted
7-
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-
    if peripheral.getType(name) == "playerDetector" then
12+
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-
    local playerDetected = #players > 0
29+
30
-- FUNCTION TO SET REDSTONE CLEANLY
31
local function setRedstone(state)
32-
        redstone.setOutput(redstoneSide, not playerDetected)
32+
    if redstoneState ~= state then
33
        redstone.setOutput(redstoneSide, state)
34-
        redstone.setOutput(redstoneSide, playerDetected)
34+
        redstoneState = state
35
        print("Redstone " .. (state and "ON" or "OFF"))
36
    end
37-
    if playerDetected then
37+
38-
        sleep(redstoneDuration)
38+
39-
        redstone.setOutput(redstoneSide, invertMode) -- Reset output after duration
39+
40
while true do
41
    local players = detector.getPlayersInRange(detectionRange)
42-
    sleep(0.01) -- Short delay to prevent excessive CPU usage
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