ccSensor, ComputerCraft Problem

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Lewis Evelyn

New Member
Jul 29, 2019
25
0
0
Right so I am no way the best coder so below is my code to open a door automatically depending who is in the room.

I got that part to work

So if me (lce123) is in the room the door is open

if me and someone else is in the room the door is open

if someone else is in the room without me the door is closed

However if no one is in the room the code won't shut the door (which is not what I want)

if that makes sense :p

Code:
p = peripheral.wrap("left")

function unlock()
    rs.setOutput("right", true)
end

while true do
os.pullEvent =os.pullEventRaw
var = 0
    players = p.getPlayerNames()
    for num, name in pairs(players) do
        if name == "lce123" then
            print("variable set to 1")
            var = 1
            sleep(1)
        end

        if var == 1  then
            print("unlocking")
            unlock()
            sleep(1)
        elseif var == 0 then
        rs.setOutput("right", false)
        end
        print(num..":"..name)
        var = 0
    end

    sleep(1)
end

So if someone knows how I can code the door close when no one is there that would be great :D
 

Arkandos

New Member
Jul 29, 2019
349
0
0
The problem you are having is that you are looping through the names of nearby players. All you have to do is
Code:
if players == nil then rs.setOutput("right", false) end
right before your last sleep.
That will make so that if there are no players detected, it will close the door. I cannot say if this will work, but theoretically it should. But next time, atleast provide which mod you are using to access nearby players. There are quite a few.