Advertisement
Ubidibity

playersdetected.lua

Jun 5th, 2025 (edited)
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | Gaming | 0 0
  1. local pd = peripheral.wrap("bottom")
  2. local mon = peripheral.wrap("top")
  3.  
  4. mon.setTextScale(0.5)
  5.  
  6. -- Define a range for checking if players are in range (in blocks)
  7. local detectionRange = 50
  8.  
  9. while true do
  10.     mon.clear()
  11.     -- Set a header for the display
  12.     mon.setCursorPos(1, 1)
  13.     mon.write("=== Player Tracker ===")
  14.  
  15.     -- Get online players, default to empty table if nil
  16.     local players = pd.getOnlinePlayers() or {}
  17.     local row = 3 -- Start displaying players from row 3 to leave space for the header
  18.  
  19.     for k, v in pairs(players) do
  20.         local playerPos = pd.getPlayerPos(v)
  21.         -- Check if the player is within the specified range using their position
  22.         local inRange = pd.isPlayerInRange(playerPos, detectionRange)
  23.         local playerData = pd.getPlayer(v)
  24.  
  25.         -- Write player name
  26.         mon.setCursorPos(1, row)
  27.         mon.write("Player: " .. v)
  28.  
  29.         -- Write position
  30.         mon.setCursorPos(1, row + 1)
  31.         mon.write("Pos: x=" .. math.floor(playerPos.x) .. " y=" .. math.floor(playerPos.y) .. " z=" .. math.floor(playerPos.z))
  32.  
  33.         -- Write health (if available in getPlayer data)
  34.         mon.setCursorPos(1, row + 2)
  35.         if playerData and playerData.health then
  36.             mon.write("Health: " .. playerData.health .. "/" .. (playerData.maxHealth or "20"))
  37.         else
  38.             mon.write("Health: N/A")
  39.         end
  40.  
  41.         -- Write if the player is in range
  42.         mon.setCursorPos(1, row + 3)
  43.         mon.write("In Range (" .. detectionRange .. " blocks): " .. (inRange and "Yes" or "No"))
  44.         if inRange then
  45.             redstone.setOutput("back", true)
  46.         else
  47.             redstone.setOutput("back", false)
  48.         end
  49.  
  50.         -- Add a separator
  51.         mon.setCursorPos(1, row + 4)
  52.         mon.write("-------------------")
  53.  
  54.         row = row + 6 -- Move to the next block of text (leaving a gap)
  55.     end
  56.  
  57.     -- If no players are online, display a message
  58.     if #players == 0 then
  59.         mon.setCursorPos(1, 3)
  60.         mon.write("No players online.")
  61.     end
  62.  
  63.     sleep(20) -- Refresh every 20 seconds
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement