s3ptum

doorProx

Sep 8th, 2025 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. -- This program detects a player named "s3ptum" within a 6-block radius
  2. -- using an Advanced Peripherals Player Detector and sends a redstone signal
  3. -- to the back of the computer if the player is present.
  4.  
  5. -- Define the player's name and the detection radius
  6. local targetPlayer = "s3ptum"
  7. local detectionRadius = 6
  8.  
  9. -- Main program loop
  10. while true do
  11. -- Use peripheral.wrap() to connect to the player detector on a specific side.
  12. -- The detector is now explicitly set to the "right" side.
  13. local playerDetector = peripheral.wrap("right")
  14.  
  15. -- Check if a peripheral was found AND if it has the required method.
  16. if playerDetector and playerDetector.getPlayersInRange then
  17. -- If the detector is found and valid, proceed with the main logic.
  18. local players = playerDetector.getPlayersInRange(detectionRadius)
  19.  
  20. local playerFound = false
  21. -- Iterate through the list of detected players.
  22. for _, player in pairs(players) do
  23. -- Check if the current player's name matches our target.
  24. if player.name == targetPlayer then
  25. playerFound = true
  26. break -- Exit the loop once the player is found
  27. end
  28. end
  29.  
  30. -- Based on whether the player was found, set the redstone signal.
  31. if playerFound then
  32. -- Send a redstone signal to the back of the computer.
  33. rs.setOutput("back", true)
  34. print("Player '" .. targetPlayer .. "' detected. Redstone ON.")
  35. else
  36. -- Turn off the redstone signal.
  37. rs.setOutput("back", false)
  38. print("Player '" .. targetPlayer .. "' not detected. Redstone OFF.")
  39. end
  40.  
  41. -- Wait for a short duration before checking again to prevent
  42. -- excessive CPU usage.
  43. sleep(0.5)
  44. else
  45. -- If the detector is not found or is the wrong type, print an error and
  46. -- a list of available peripherals to help with debugging.
  47. print("Could not find a valid player detector.")
  48. print("Please check that the correct block is connected to the 'right' side.")
  49. print("Available peripherals:")
  50. for _, p in pairs(peripheral.getNames()) do
  51. print("- " .. p)
  52. end
  53. print("Trying again in 5 seconds.")
  54. sleep(5)
  55. end
  56. end
  57.  
Advertisement
Add Comment
Please, Sign In to add comment