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

Openccsensor Table Issue

Started by Sjaelen, 31 July 2013 - 03:14 PM
Sjaelen #1
Posted 31 July 2013 - 05:14 PM
Hey all, I'm having a bit of an issue with openCCsensors and receiving results from the table. I have the following code:



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

local mon = peripheral.wrap("back")
local prox = sensor.wrap("top")
local targets = prox.getTargets()

term.redirect(mon)
term.clear()

while true do
term.setCursorPos(1,1)
for target in pairs(targets) do
  local info = prox.getTargetDetails(target)
  if info.Name == "Player" then
   print("Player: "..info.Username)
   print("( "..info.Position.X..", "..info.Position.Y..", "..info.Position.Z.. " )")
   print("-------------------------------------------------------------")
   sleep(0.5)
  end
end
end

It works fine until a player enters or leaves the area that the proximity sensor can detect, and then it returns an error "startup:14: attempt to index ? (a nil value)", which from what I understand is the code not being able to find the Name in the table that openCCsensors provides.

How would I go about fixing this so the code doesn't break when someone enters/leaves, and have it update to show the change?
Lyqyd #2
Posted 01 August 2013 - 12:32 AM
Split into new topic.

Move the `local targets = prox.getTargets()` line to the beginning of the while loop, so that it will refresh the target list each time. Also, move the `sleep(0.5)` to just before the final end to have it sleep per iteration rather than per target.