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

OpenPeripherals Sensor Door

Started by TheEisbaer, 05 April 2015 - 03:40 PM
TheEisbaer #1
Posted 05 April 2015 - 05:40 PM
I am trying to make a door that uses the seonsor from OpenPeripherals so I (TheEisbaer) and my friend (Eisbaer68) can enter our house.

This is what I got so far:

list={{name = "TheEisbaer"},{name = "Eisbaer68"}}
nearbyPlayers=peripheral.wrap("right").getPlayers()

for i=1,<how many players are arround> do
for j=1,2 do
if list[j]==nearbyPlayers[i][2] then
print("success!")
rs.setOutput("left",true)
sleep(2)
rs.setOuput("left",false)
else
print("failure!")
end
end
end

So how do I make that working:
  • How do I detect how many players are arround?
  • How do I make it working only for me and my friend?
Thanks in advance.

EDIT:

I tried a bit in Ideone.com with this code:

players={{uuid = "abc",name = "TheEisbaer"},{uuid= "def",name ="Eisbaer68"},{uuid= "ghj",name = "kevin"}}
list={name ="TheEisbaer",name = "Eisbaer68"}
for i=1,3 do
for j=1,2 do
  if list[j]==players[i][2] then
   print("success!")
  else
   print("failure!")
  end
  print(i,j)
end
end

And somehow it alway says success! though it should say failure! at least at some point.
Edited on 05 April 2015 - 04:10 PM
HPWebcamAble #2
Posted 05 April 2015 - 07:44 PM
The two bits of code you posted aren't really related, so I'll address them seperately

A few things to keep in mind:
Spoiler

test = { name = "hi", name = "bye" }
That gives 'test' a single entry ("bye"), since you override 'name'

You want to do this instead:

test = { "hi", "bye" }

And to get the length of a table (Also works with strings):

test = { "hi", "bye" }
print(#test) --#> 2

Now, the sensor program is a good start.
SpoilerI actually wrote one a while ago for my own base, here is the pastebin: http://pastebin.com/JXn1eZta
It doesn't actually look for a certain player, it opens the door when ANY player is near.
It shouldn't be too hard to modify to accept a whitelist

Your code would work too, it just needs some touching up

local list = { --# The people who will trigger the door to open
  TheEisbaer = true,
  Eisbaer68 = true
}

local sensor = peripheral.wrap("right")

while true do --# Continue forever, stop with ctrl+r or ctrl+t

  local names = sensor.getPlayerNames() --# Ask the sensor for a list of the players in range

  for i=1, #names do --# Continues for the length of names

	if list[ names[i] ] then --# Checks if there is an entry in 'list' for the player name
	  print("success!")
	  rs.setOutput("left",true)
	  sleep(2)
	  rs.setOuput("left",false)
	end

  end

  sleep(2) --# Make sure to add some delay, at least a second

end


The second bit of code you posted isn't necessary for the senor code to work, but I'll try to help you with it anyway:
SpoilerSo basically, this code goes through a table to check for an entry:

players = { --# The table to check
  {uuid = "abc",name = "TheEisbaer"},
  {uuid= "def",name ="Eisbaer68"},
  {uuid= "ghj",name = "kevin"}
}

list = { --# The table of things to check for
  "TheEisbaer",
  "Eisbaer68"
}

for i=1, #list do --# Go through all the elements in 'list'

  for j=1, #players do --# Compare it to all elements in 'players'
	if list[i]==players[j]["name"] then --# players[j][(number)] doesn't work, since you used name = "name" and uuid = "uuid"
	 print("success!")
	else
	 print("failure!")
	end
	print(i..","..j)
  end

end
Edited on 05 April 2015 - 05:47 PM
TheEisbaer #3
Posted 05 April 2015 - 08:35 PM
Thank you VERY much for your help :)/>