Jump to content




OpenPeripheral: detect nearest player


  • You cannot reply to this topic
4 replies to this topic

#1 Len

  • New Members
  • 2 posts

Posted 10 May 2014 - 08:32 PM

Hello everyone,

I have a program that open doors. It uses the getPlayerNames() command. This command detects all players within his range. I only want to detect the name of the nearest player to the sensor. How do I do this?

s = peripheral.wrap("top")
for i,v in pairs(s.getPlayerNames()) do
   print(v)
end
-- It detects all players within the range. But how to only detect the nearest?

Thanks in advance,
Len

#2 CometWolf

  • Members
  • 1,283 posts

Posted 11 May 2014 - 01:40 AM

There's a command to get the distance of the player from the sensor. Just add the 3 values it returns(x,y,z) together and compare them in a loop. Whichever has the lowest value, is the nearest player.

#3 Len

  • New Members
  • 2 posts

Posted 11 May 2014 - 08:52 AM

Thanks for the reply.

Wich command is it? Can you give me an example?

#4 CometWolf

  • Members
  • 1,283 posts

Posted 11 May 2014 - 10:07 AM

I forgot what it is, it's probably just stored in the table it returns actually. Check the commands using the docs program included with openP.

Edited by CometWolf, 11 May 2014 - 10:07 AM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 May 2014 - 10:55 AM

the method you must call is getPlayerData(username) which will return a table of information about the player. In said table there is a sub-table stored under the key 'position' which will contain the players location relative to the sensor, under the keys 'x', 'y', and 'z'.

Edited by theoriginalbit, 11 May 2014 - 10:56 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users