Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This program detects a player named "s3ptum" within a 6-block radius
- -- using an Advanced Peripherals Player Detector and sends a redstone signal
- -- to the back of the computer if the player is present.
- -- Define the player's name and the detection radius
- local targetPlayer = "s3ptum"
- local detectionRadius = 6
- -- Main program loop
- while true do
- -- Use peripheral.wrap() to connect to the player detector on a specific side.
- -- The detector is now explicitly set to the "right" side.
- local playerDetector = peripheral.wrap("right")
- -- Check if a peripheral was found AND if it has the required method.
- if playerDetector and playerDetector.getPlayersInRange then
- -- If the detector is found and valid, proceed with the main logic.
- local players = playerDetector.getPlayersInRange(detectionRadius)
- local playerFound = false
- -- Iterate through the list of detected players.
- for _, player in pairs(players) do
- -- Check if the current player's name matches our target.
- if player.name == targetPlayer then
- playerFound = true
- break -- Exit the loop once the player is found
- end
- end
- -- Based on whether the player was found, set the redstone signal.
- if playerFound then
- -- Send a redstone signal to the back of the computer.
- rs.setOutput("back", true)
- print("Player '" .. targetPlayer .. "' detected. Redstone ON.")
- else
- -- Turn off the redstone signal.
- rs.setOutput("back", false)
- print("Player '" .. targetPlayer .. "' not detected. Redstone OFF.")
- end
- -- Wait for a short duration before checking again to prevent
- -- excessive CPU usage.
- sleep(0.5)
- else
- -- If the detector is not found or is the wrong type, print an error and
- -- a list of available peripherals to help with debugging.
- print("Could not find a valid player detector.")
- print("Please check that the correct block is connected to the 'right' side.")
- print("Available peripherals:")
- for _, p in pairs(peripheral.getNames()) do
- print("- " .. p)
- end
- print("Trying again in 5 seconds.")
- sleep(5)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment