The problem you are having is that you are looping through the names of nearby players. All you have to do is
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...