The OpenPeripharalsAddons sensor can get relative position data of nearby players. It can also get all nearby players in case you want it to work for anybody.
This will get all nearby players:
local sensor = peripheral.wrap("top") -- The peripheral doesn't have to be on the top, I'm just using the top side as an example
local players = sensor.getPlayers()
It returns a table with this structure:
{
{
uuid = "4afdca7f-0fed-456b-9215-b74e6f0a1a15",
name = "examplePlayer"
},
{
uuid = "b63b9bda-8d65-48e1-9b22-381cd0ef3e04",
name = "anotherPlayer"
}
}
You can then get the position information of each respective player by using something like this
local position = sensor.getPlayerByName("examplePlayer").basic().position
-- OR you can use their UUID
local position = sensor.getPlayerByUUID("4afdca7f-0fed-456b-9215-b74e6f0a1a15").basic().position
Obviously, if you are doing all nearby players you wouldn't hard-code like that, but you get the idea.
Also, this is what the content of position might look like
{
x = 2.510653,
y = -1,
z = 0.542056
}
Don't forget that the contents of the position table are
relative to the sensor's position.
Also, you probably want to check whether the player is
close enough to the block you are checking if they are in, so you would perform some sort of distance threshold calculation to check for that. The following is an example of what I mean by that:
local position = sensor.getPlayerByName("examplePlayer").basic().position
-- Let's say we want the player to be more or less 2 blocks above the sensor
local target = {x = 0, y = 2, z = 0}
-- We're going to use the 3d distance formula to calculate the distance from the player to the target position
local distance = math.sqrt(
(target.x - position.x)^2 +
(target.y - position.y)^2 +
(target.z - position.z)^2 )
if distance <= 0.5 then -- So if the player is less than or is half a block from the target
redstone.setOutput("back", false) -- Again, back could be any side
else -- Or else, the player is more than half a block from the target, so that doesn't count
redstone.setOutput("back", true)
end
Hope I helped!