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

OpenPeripheralsAddons Sensor help

Started by ligglo, 28 March 2014 - 12:23 AM
ligglo #1
Posted 28 March 2014 - 01:23 AM
I have been trying to get this to open a door when I am nearby. I am also wondering if there is a way to lower the range of the sensor so it will only open when I am within two blocks of it. At the moment the code that I have doesn't function properly. It works when I am there, but when I'm not it won't close. I am using a TiCo drawbridge as the door, so it is inverted.


m = peripheral.wrap("right")

while true do

sleep(1)
names = m.getPlayerNames()
for i = 1, #names do
  if names[i] == "ligglo" then
	redstone.setOutput("left", false)
  else
	redstone.setOutput("left", true)
  end
end
end
Bomb Bloke #2
Posted 28 March 2014 - 01:43 AM
If no players are nearby, then "names" will be an empty table; "#names" will hence be 0, and so your loop won't run at all. Add in an additional check to handle that situation.

(This is working under the assumption that "getPlayerNames()" doesn't block until at least one player is in range.)

Bear in mind that the way you've written things, if another player approaches the door it'll close, even if you're also nearby. I'm unsure as to whether that's intended behaviour or not.

I'm afraid I've not tinkered with the sensor, and am unaware whether it's possible to mess with its range or check the distance of detected players.
Edited on 28 March 2014 - 12:45 AM
ligglo #3
Posted 28 March 2014 - 02:20 AM
Thanks a ton, here is the fixed code for anyone running in to the same issue.


m = peripheral.wrap("right")

while true do

  sleep(1)
  names = m.getPlayerNames()

  if names[1] == nil then
    redstone.setOutput("left", true)
    else for i = 1, #names do
      if names[i] == "ligglo" then
        redstone.setOutput("left", false)
      end
    end
  end
end
Edited on 28 March 2014 - 01:21 AM