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

GAH Help. Proximity Sensor Issue.

Started by ProfessorTenebrae, 26 February 2014 - 07:21 AM
ProfessorTenebrae #1
Posted 26 February 2014 - 08:21 AM
I've ben trying to get a forcefield to turn off when a player gets near but I can't seem to find ANY way of doing this.
I have a redstone torch attached to the computer. I just need a signal to come on if a player, any player is present

So I would like 2 things here…

How would I solve this?

And what variables can I take from the proximity sensor commands and how would I do so? Since I keep getting confused about it.

My current attempt so far isn't…great:

os.loadAPI("ocs/apis/sensor")
prox = sensor.wrap("back")

while true do
for name, details in pairs(targets)

if details.name == nil then
redstone.setOutput("left",false)
else
redstone.setOutput("left",true)
end
end
CometWolf #2
Posted 26 February 2014 - 07:16 PM
It's been a long ass time since i've worked with the ocs sensors, but i believe you're getting the info incorrectly.
Also, im guessing that what you posted is not the actual code, as that would probably error.

local tDetails, tEntities = {} --tables for storing info
for name,table in pairs(sensor.getTargets()) do
  tDetails[#tEntities+1] = sensors.getTargetDetails(name)
  tEntities[#tEntities+1] = name
end
for i=1,#tEntities do --avoids error in case the player moves out of range
  if not tDetails[i] then
	sleep(0.5)
	break
  end
  -- players
  local userName
  if tDetails[i]["Name"] == "Player" then -- the name of a player entity is just player, to get the username we need to check the Username field
   userName = tDetails[i]["Username"]
  end
end
You could probably just do something like this though

os.loadAPI("ocs/apis/sensor")
prox = sensor.wrap("back")
while true do
  for name, details in pairs(prox.getTargets())
    redstone.setOutput("left",true)
  end
  sleep(0.5)
  rs.setOutput("left",false)
end


Also, to quickly check what contents are avilable in a given table, do the following

for k,v in pairs(table) do
  print(k)
end
Edited on 26 February 2014 - 06:19 PM
ProfessorTenebrae #3
Posted 27 February 2014 - 09:37 AM
Thanks! That works perfectly thank you! :D/>/>

Although I'm trying to make it pause for 5 seconds before sending the redstone output. How can I do this?
CometWolf #4
Posted 27 February 2014 - 04:19 PM

os.loadAPI("ocs/apis/sensor")
local prox = sensor.wrap("back")
while true do
  local acess = false
  for name, details in pairs(prox.getTargets())
    acess = true
    break
  end
  if acess then
    sleep(5)
    rs.setOutput("left",true)
  else
    rs.setOutput("left",false)
    sleep(0.5)
  end
end
ProfessorTenebrae #5
Posted 28 February 2014 - 04:09 AM
Thanks! :D/>