This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Len's profile picture

OpenPeripheral: detect nearest player

Started by Len, 10 May 2014 - 07:32 PM
Len #1
Posted 10 May 2014 - 09: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
CometWolf #2
Posted 11 May 2014 - 02: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.
Len #3
Posted 11 May 2014 - 09:52 AM
Thanks for the reply.

Wich command is it? Can you give me an example?
CometWolf #4
Posted 11 May 2014 - 11: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 on 11 May 2014 - 09:07 AM
theoriginalbit #5
Posted 11 May 2014 - 11: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 on 11 May 2014 - 09:56 AM