BigBlow_

playerDetector

Oct 28th, 2025
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. -- player_detector70 --
  2. -- CONFIGURATION --
  3. local detectionRange = 70      -- Detection range in blocks
  4. local redstoneSide = "front"   -- Side where redstone signal is sent
  5. local checkDelay = 2           -- Delay between each detection (in seconds)
  6.  
  7. -- FINDING THE PLAYER DETECTOR AUTOMATICALLY --
  8. local detector = nil
  9. for _, name in ipairs(peripheral.getNames()) do
  10.     if peripheral.getType(name) == "player_detector" then
  11.         detector = peripheral.wrap(name)
  12.         break
  13.     end
  14. end
  15.  
  16. if not detector then
  17.     print("No player detector found!")
  18.     return
  19. end
  20.  
  21. print("Player detector found on " .. peripheral.getName(detector))
  22. print("Monitoring players in range " .. detectionRange .. "...")
  23.  
  24. -- STATE TRACKING --
  25. local redstoneState = false
  26.  
  27. -- FUNCTION TO SET REDSTONE CLEANLY --
  28. local function setRedstone(state)
  29.     if redstoneState ~= state then
  30.         redstone.setOutput(redstoneSide, state)
  31.         redstoneState = state
  32.         print("Redstone " .. (state and "ON" or "OFF"))
  33.     end
  34. end
  35.  
  36. -- MAIN LOOP --
  37. while true do
  38.     local players = detector.getPlayersInRange(detectionRange)
  39.     local detected = #players > 0
  40.  
  41.     if detected then
  42.         setRedstone(true)
  43.     else
  44.         setRedstone(false)
  45.     end
  46.  
  47.     sleep(checkDelay)
  48. end
  49.  
Advertisement
Add Comment
Please, Sign In to add comment