I'm using a OpenCCSensors proximity sensor to trigger a redstone signal with the bewlow code… Sometimes this line of code


	local alldetails = proxim.getTargetDetails (name)

returns a nil value. Any ideas why this happens? I am NOT moving out of range… I know I can work around this by checking for a nil value but I'm curious as to what happens here…


os.loadAPI("ocs/apis/sensor")

local scandelay = 2
local sensorside = "top"
local proxim = sensor.wrap (sensorside)

local monitorside = 'left'
local monitorscale = 0.5
local monitor = nil
local maxx, maxy = 0

local redstoneside = 'right'

local triggerarea = {}
triggerarea [1] = { x = { min = -3, max = 4 }, y = { min = 5, max = 7 }, z = { min = 4, max = 9 } }

------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------

function initializemonitor (myside, myscale)
local mymon = peripheral.wrap (myside, myscale)

if (mymon == nil) then
return nil
end

mymon.clear ()
mymon.setTextScale (myscale)
local mymaxx, mymaxy = mymon.getSize ()

return mymon, mymaxx, mymaxy
end

------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------

monitor, maxx, maxy = initializemonitor (monitorside, monitorscale)

term.redirect (monitor)

while (true) do
local targets = proxim.getTargets ()

  for name, basicdetails in pairs (targets) do
   if (basicdetails.Name == "Player") then
	local alldetails = proxim.getTargetDetails (name)
  
	for index, postable in ipairs (triggerarea) do
	 if (((postable.x.min <= alldetails.Position.X) and (postable.x.max >= alldetails.Position.X)) and ((postable.y.min <= alldetails.Position.Y) and (postable.y.max >= alldetails.Position.Y)) and ((postable.z.min <= alldetails.Position.Z) and (postable.z.max >= alldetails.Position.Z))) then
	   if (not redstone.getOutput (redstoneside)) then
		redstone.setOutput(redstoneside, true)
		print (name.." [open]")
	   end
	 else
	   if (redstone.getOutput (redstoneside)) then
		redstone.setOutput("right", false)
		print (name.." [closed]")
	   end
	 end
	end
   end
  end

sleep (scandelay)
end

term.restore ()