Advertisement
Cr1spyBacon8r

Computercraft Player detector

Nov 20th, 2024 (edited)
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. -- The name of the player to check for
  2. local targetPlayer = "PlayerName"
  3.  
  4. -- Redstone output side (right)
  5. local redstoneSide = "right"
  6.  
  7. -- Get the peripheral for the player detector, default to 'back'
  8. local playerDetector = peripheral.wrap("back") -- Adjust the peripheral name if needed
  9.  
  10. -- Check if playerDetector is connected
  11. if not playerDetector then
  12.     error("Player detector peripheral not found on 'back'. Please check the connection.")
  13. end
  14.  
  15. -- Function to display the splash screen
  16. function displaySplashScreen(isPlayerOnline)
  17.     if isPlayerOnline then
  18.         term.setBackgroundColor(colors.green)
  19.         term.setTextColor(colors.white)
  20.         term.clear()
  21.         term.setCursorPos(1, 1)
  22.        
  23.         local screenWidth, screenHeight = term.getSize()
  24.         local message = "Lag Reducer 3001"
  25.         local status = targetPlayer .. " is ONLINE!"
  26.         local x1 = math.floor((screenWidth - #message) / 2)
  27.         local y1 = math.floor(screenHeight / 2) - 1
  28.         local x2 = math.floor((screenWidth - #status) / 2)
  29.         local y2 = math.floor(screenHeight / 2) + 1
  30.        
  31.         term.setCursorPos(x1, y1)
  32.         term.write(message)
  33.         term.setCursorPos(x2, y2)
  34.         term.write(status)
  35.     else
  36.         term.setBackgroundColor(colors.red)
  37.         term.setTextColor(colors.white)
  38.         term.clear()
  39.         term.setCursorPos(1, 1)
  40.        
  41.         local screenWidth, screenHeight = term.getSize()
  42.         local message = "Lag Reducer 3001"
  43.         local status = targetPlayer .. " is OFFLINE."
  44.         local x1 = math.floor((screenWidth - #message) / 2)
  45.         local y1 = math.floor(screenHeight / 2) - 1
  46.         local x2 = math.floor((screenWidth - #status) / 2)
  47.         local y2 = math.floor(screenHeight / 2) + 1
  48.        
  49.         term.setCursorPos(x1, y1)
  50.         term.write(message)
  51.         term.setCursorPos(x2, y2)
  52.         term.write(status)
  53.     end
  54. end
  55.  
  56. -- Function to check if the target player is online
  57. function checkPlayerOnline()
  58.     -- Get a list of online players from the playerDetector peripheral
  59.     local onlinePlayers = playerDetector.getOnlinePlayers()
  60.  
  61.     -- Check if the target player is in the list of online players
  62.     for _, player in ipairs(onlinePlayers) do
  63.         if player == targetPlayer then
  64.             return true -- Player is online
  65.         end
  66.     end
  67.     return false -- Player is not online
  68. end
  69.  
  70. -- Function to set the redstone output based on player status
  71. function setRedstoneOutput(isPlayerOnline)
  72.     if isPlayerOnline then
  73.         redstone.setOutput(redstoneSide, true) -- Emit redstone signal
  74.     else
  75.         redstone.setOutput(redstoneSide, false) -- Turn off redstone signal
  76.     end
  77. end
  78.  
  79. -- Continuously check every few seconds
  80. while true do
  81.     local isOnline = checkPlayerOnline() -- Check if the player is online
  82.     displaySplashScreen(isOnline) -- Show splash screen with player's status
  83.     setRedstoneOutput(isOnline) -- Set the redstone output accordingly
  84.     sleep(5) -- Check every 5 seconds
  85. end
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement