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

(Tables) Openperiheral sensor door

Started by jarifle, 29 March 2015 - 09:00 AM
jarifle #1
Posted 29 March 2015 - 11:00 AM
Hello,

So i'm trying to make a door with a blacklist, whitelist and unknow list but i've got some problems

1. When there is no player nearby it says;"Attempt to index?".but when there is a player when the program starts it does work and reply as wanted
2. i would like some help with the black and whitelist part i've looked around on the internet for days and i still don't get how its suppost to work
3. if you find a thing that can be done beter tell me i'm open for it.

here is the code:


local s = peripheral.wrap("left")
local speaker = peripheral.wrap("right")
function getInfo()
  info = s.getPlayers()
  return info
end
function getUsername()
  getInfo()
  info = s.getPlayers()
  username = info[1].name
  return username
end
while true do
getInfo()
getUsername()

if username == "jarifle" then
  speaker.speak("Welcome home master! How are you today?", 15, "en")
  sleep(2.8)
  speaker.speak("Let me just open the base for you.", 15, "en")
  rs.setOutput("back", true )
  sleep(10)
  shell.run("reboot")
elseif username == "Prince_Stark" then
  speaker.speak("Welcome guest! Acces to the base has been granted", 15, "en")
  sleep(3)
  speaker.speak("Opening the base.", 15, "en")
  rs.setOutput("back", true )
  sleep(10)
  m.clear()
  shell.run("reboot")
end
sleep(1)
end

jarifle is me and prince_stark is someone i want on the whitelist.

if someone could help me i would be so happy.

Thank you.

PS.sorry for spelling and grammar i'm form belgium.
Edited on 29 March 2015 - 10:13 AM
KingofGamesYami #2
Posted 29 March 2015 - 03:50 PM
For a whitelist, you'd do something like this:

local whitelist = {
  jarifle = true,
  Prince_Stark = true,
}

And then to check if someone is on the whitelist:

if whitelist[username] then
--#they're on it
else
--#they're not on it
end

Attempt to Index means you are trying to access a variable as if it were a table, but it is not. Most likely, s.getPlayers() returns a table of nearby players, or nil if no players are specified.

Also, you should use local variables when possible, as they are accessed faster and cannot be modified by external programs. Another thing to do is to replace shell.run with the actual code. ei shell.run("reboot") can be replaced with os.reboot()

Here's the code, with some changes

--#wrap peripherals
local s = peripheral.wrap("left")
local speaker = peripheral.wrap("right")
--#make a whitelist
local whitelist = {
  jarfile = true,
  Prince_Stark = true,
}

while true do
  local users = s.getPlayers()
  if users then --#make sure users isn't nil
    for _, user in ipairs( users ) do --#iterate through all the users
      if whitelist[ user.name ] then --#if the user is on the whitelist
        --#say stuff
        break --#exit the for loop early
      end
    end
  end
  sleep( 1 ) --#wait one second
end
jarifle #3
Posted 29 March 2015 - 04:33 PM
Oke this works like a charm now it fixed alot of the issues thanks for that you saved my project.

And i guesse that now if i want more lists like say a master grey-and blacklist i just do "elseif" behind the "if" of whitelist?

Once again thanks alot.
KingofGamesYami #4
Posted 29 March 2015 - 05:21 PM
No, if you wanted to have different "groups" of users, you'd compare the top level first - something like this:

if (user is admin) then

elseif (user is mod) then

elseif (user is whitelisted) then

end

(Obviously this isn't actual code)
jarifle #5
Posted 29 March 2015 - 06:07 PM
Oh oke, thanks again.

1 more question: How would you go about making the system react to 2 players?
For example: whitelisted + blacklisted = closed
whitelisted + whitlisted = open

or will that be to complex?
HPWebcamAble #6
Posted 29 March 2015 - 06:43 PM
Oh oke, thanks again.

1 more question: How would you go about making the system react to 2 players?
For example: whitelisted + blacklisted = closed
whitelisted + whitlisted = open

or will that be to complex?

King's code checks every user that the sensor detected, and when at least one is on the whitelist, it'll open the door.

What you could do instead is look through every user found for someone who ISN'T whitelisted, then you know that the door shouldn't open
jarifle #7
Posted 29 March 2015 - 06:52 PM
I figured that was how it works but it doens't really mater it was just out of intrest.

I thank both of you for the help.