Pandafuchs

Find Nearest Player

Sep 22nd, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI("ocs/apis/sensor")
  2. mySensor = sensor.wrap("left")
  3.  
  4. while true do
  5.     targets = mySensor.getTargets()
  6.     player = false;
  7.     for k, v in pairs( targets ) do
  8.         details = mySensor.getTargetDetails(k)
  9.         if details ~= nil and details["IsPlayer"] ~= nil and details["IsPlayer"] then
  10.             pos = details["Position"];
  11.             dist = math.sqrt(math.pow(pos["X"],2) + math.pow(pos["Y"],2) + math.pow(pos["Z"],2))
  12.             if dist <= 40 then
  13.                 player = true
  14.             end
  15.         end
  16.     end
  17.  
  18.     if player then
  19.         redstone.setOutput("right", true)
  20.     else
  21.         redstone.setOutput("right", false)
  22.     end
  23. end
Advertisement
Add Comment
Please, Sign In to add comment