Jump to content




How to count people inside the sensor radius


1 reply to this topic

#1 KrisRevi

  • Members
  • 14 posts

Posted 29 June 2016 - 09:50 AM

Hi.

This is my code for a sensor opening door lock system and it's working perfect! BUT i was wondering how i could count the number of entitys/players inside the radius of the sensor and stop the script from running and tell them "s.speak("Only 1 person at a time")"?

sub.question, is it possible to set the radius for the openCCsensor sensor?

os.loadAPI("sensor")
prox = sensor.wrap("left")
s = peripheral.wrap("right")
while true do
local targets = prox.getTargets()
local moreDetails = prox.getTargetDetails(name)
s.speak("Please step onto marked area and stand still")
sleep(5)
for name, basicDetails in pairs(targets) do
 
  s.speak("Scanning Entity")
  sleep(3)
 
  if name == "KrisRevi" then
  s.speak("Access Granted, welcome KrisRevi")
  sleep(10)
  elseif name == "UNLEASHED91" then
  s.speak("Access Granted, welcome Unleashed")
  sleep(10)
  end
 
end
end


#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 29 June 2016 - 05:00 PM

I can't see how that'd be "working perfectly", as the variable name doesn't seem to be set when it's passed to .getTargetDetails on line 6.

In any case, you'd count the targets the same way you iterate them, except you'd increment a counter instead of checking their names.
local target_count = 0
for name, basicDetails in pairs(targets) do
	target_count = target_count + 1
end

Then you'd only let through anyone if target_count was 1.

There's also room for optimization in your code. You could just have a table at the top that'd hold all the players to whom access is to be granted, and the infinite loop should iterate over that table and check its entries against targets.

Edited by LBPHacker, 29 June 2016 - 05:04 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users