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

automated door

Started by aisha_girl_15, 04 April 2014 - 06:53 AM
aisha_girl_15 #1
Posted 04 April 2014 - 08:53 AM
hello everyone,

I have a question about cc, i'm not good at it but i'm trying to learn it.

I'm making a program to automatically open the door of my house ( castle ) if a player approaches.

I use a sensor at the left side of the advanced computer and the redstone output is at the back of the computer
this is the code I wrote and the error it gave.
Can someone help me?

os.loadAPI("sensor")
prox = sensor("left")

while true do
t = prox.getTargets(name)
if t == "aisha_girl_15" then
rs.setOutput("back",true)
wait(15)
rs.setOutput("back",false)
else
wait(5)
end
end

and the error it gave:
file not found:
doorway:2: attempt to index ? ( a nill value)
Cranium #2
Posted 04 April 2014 - 03:45 PM
Moved to Ask A Pro.
Make sure you're posting questions in the right area. Your questions get more exposure in this section, so that we can help you easier.
CometWolf #3
Posted 04 April 2014 - 04:10 PM
Like the error says, this

prox = sensor("left")
Is not valid. Where did you pick this up?
The proper code would be

local prox = peripheral.wrap"left"

This also won't work

t = prox.getTargets(name)
That name variable is listed there for a reason, not because it expects a variable called name. Although i don't believe the getTargets function uses any arguments at all.

Regardless, the result of getTargets() is not a string, so this won't work

if t == "aisha_girl_15" then
It returns a numerically indexed table, meaning you'll have to loop through it looking for the desired name

for i=1,#t do --loop once for every key in the table t
  if t[i] == "aisha_girl_15" then --compare the value stored in the table as the current loopCount
    --open door here
    break --end loop
  end
end
I haven't worked with the sensor in ages, but chances are the actual player name is stored inside another table that's stored in the numerical key. Probably something like

t[i].playerName
aisha_girl_15 #4
Posted 04 April 2014 - 05:15 PM
first i will move it to the proper forum…

and thank you for the answer, i will test it out.

for that part of code…
i found that code on internet