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

CCSensor won't accept 2 whitelisted players?

Started by mattie078, 21 June 2014 - 01:37 PM
mattie078 #1
Posted 21 June 2014 - 03:37 PM
When I put 2 whitelisted people in the script, it won't work? Can somebody help me?


os.loadAPI("ocs/apis/sensor")
prox = sensor.wrap("right")
names = "test"
whitelist = {"samduuren"}
while true do
term.clear()
term.setCursorPos(1,1)
  local targets = prox.getTargets()
  for name,basicDetails in pairs(targets) do
	for key,value in pairs(basicDetails) do
	  if basicDetails.Name == "Player" then
	   detected = "true"
	   print(name)
		for i = 1,#whitelist do
		  if name == whitelist[i] then
			rs.setOutput("left", false)
		  else
		  if name ~= whitelist[i] then
			rs.setOutput("left", true)
			  sleep(0.5)
			rs.setOutput("left", false)
			  end
			end
		  end
		end
	  end
	end
  end
KingofGamesYami #2
Posted 21 June 2014 - 05:28 PM
When I put 2 whitelisted people in the script, it won't work? Can somebody help me?


os.loadAPI("ocs/apis/sensor")
prox = sensor.wrap("right")
names = "test"
whitelist = {"samduuren"}
while true do
term.clear()
term.setCursorPos(1,1)
  local targets = prox.getTargets()
  for name,basicDetails in pairs(targets) do
	for key,value in pairs(basicDetails) do
	  if basicDetails.Name == "Player" then
	   detected = "true"
	   print(name)
		for i = 1,#whitelist do
		  if name == whitelist[i] then
			rs.setOutput("left", false)
		  else
		  if name ~= whitelist[i] then
			rs.setOutput("left", true)
			  sleep(0.5)
			rs.setOutput("left", false)
			  end
			end
		  end
		end
	  end
	end
  end
So, I'm guessing it triggers the wrong effect here:

for i = 1, #whitelist do
 if name == whitelist[i] then
  rs.setOutput("left", false)
 else --#btw, use elseif for this
  if name ~= whitelist[i] then --#because this statement is within the loop, if the name does not match the first name in the whitelist, it will be true unless it is the first person
   rs.setOutput("left", true) 
   sleep(0.5)
   rs.setOutput("left", false)
  end
 end
end

I would propose
Spoiler

function compare( name ) --#this function will return true if the name is in the whitelist
 for i = 1, #whitelist do
  if whitelist[i] == name then
   return true --#this will return true if the name is the same
  end
 end
 return false --#this line will not even be called unless none of the whitelisted people are in range
end
--#now, to insert this function in your script
print(name)
if compare(name) then --#boolean variables can be used directly in statements.
 rs.setOutput("left", false)
else
 rs.setOutput("left", true)
 sleep(0.5)
 rs.setOutput("left", false)
end
--#etc.
theoriginalbit #3
Posted 21 June 2014 - 05:52 PM
I suggest an alternate, and more efficient solution over the one that KingofGamesYami proposed. if you use the whitelisted player's name as a key for the whitelist table it will mean no loop (ergo much more efficient) to check if the player is whitelisted. you would end up with code like so


os.loadAPI("ocs/apis/sensor")
local prox = sensor.wrap("right")
local whitelist = {
  ["samduuren"] = true,
  ["theoriginalbit"] = true,
  --# etc
}

while true do
  term.clear()
  term.setCursorPos(1,1)
  local targets = prox.getTargets()
  for name, details in pairs(targets) do
    --# your loop over basic details was no used, it was a pointless loop
    --# I wasn't too sure what the basicDetails.Name comparison was for either
    if whitelist[name] then
      rs.setOutput("left", false)
    else
      rs.setOutput("left", true)
      sleep(0.5)
      rs.setOutput("left", false)
    end
  end
end

take note of the few comments I left in the code. one other thing I should point out was this

if name == whitelist[i] then
  --# code
else
  if name ~= whitelist[i] then
    --# code
  end
end
KingofGamesYami suggested that you use an elseif instead making it


if name == whitelist[i] then
  --# code
elseif name ~= whitelist[i] then
  --# code
end
however even this is incorrect, you'd simply use `else` this is because you're checking if it is equal in the first `if` condition, if its not equal then it can only be not equal, meaning there is no need for the `elseif`

if condition then
  --# condition was true
else
  --# condition was false
end
compared to

if condition1 then
  --# condition1 was true
elseif condition2 then
  --# condition1 was false, condition2 was true
else
  --# condition1 and condition2 were false
end
mattie078 #4
Posted 21 June 2014 - 06:46 PM
Thank you it worked, needed some tweaking still but it is working great now! This is what I have now.
(Btw, that basicDetails.Name was for that the redstone won't trip when scanning mobs only players)


os.loadAPI("ocs/apis/sensor")
local prox = sensor.wrap("right")
local whitelist = {
  ["samduuren"] = true,

}

while true do
  term.clear()
  term.setCursorPos(1,1)
  local targets = prox.getTargets()
  for name, basicDetails in pairs(targets) do
    if basicDetails.Name == "Player" then
    print(name)
	  if whitelist[name] then
	  rs.setOutput("left", false)
    else
	  rs.setOutput("left", true)
	  sleep(0.5)
	  rs.setOutput("left", false)
	  end
    end
  end
end