Good, what I wrote earlier is one of the simpler solution but need a few changes and comments
Spoiler
os.unloadAPI("/ocs/apis/sensor") -- taken from original code, probably a reson to try and remove the api and add it again
os.loadAPI("/ocs/apis/sensor") -- makes it possible to use the sensor api in the program
local prox = sensor.wrap("top") -- make something on the top of the computer usable under the name prox
local whitelist = {"Stekeblad", "Karaktar"} -- names of all the player we want to allow
local playersThisScan = {}
local playersLastScan = {}
local function getPlayersInRange() -- returns a table containing the names of all players in range
local playersFound = {}
local targets = prox.getTargets() -- scan for stuff
for k, v in pairs(targets) do -- check everything found one at the time
if((v.Name) == "Player") then -- make sure it is a player, can the sensor find items?
targetsDetail = prox.getTargetsDetail() -- get more info about the player
if not(targetsDetail == nil) then
playerName = targetsDetail.Username -- get the players name
playersFound[playerName] = true
end
end
end
return playersFound
end
local function filterOnWhitelistedPlayers(whitelist, tableToFilter) -- takes two tables with names, returns all names that is in both tables (everyone that is whitelisted)
local filteredTable = {}
for k, v in pairs(tableToFilter) do -- for all found players
if whitelist[k] then
filteredTable[k] = v -- save all players that is in in the whitelist
end
end
return filteredTable
end
local function shouldDoorOpen() -- do the check if the door should open here
for k, v in pairs(playersThisScan) do
if not (playersLastScan[k]) then -- if player( that is whitelisted) is in range of the scanner and was not in range last scan
return true -- allow the door to be opened
end
end
return false -- do not open the door
end
while true do
playersLastScan = playersThisScan -- remember all whitelisted players that was in range
playersThisScan = getPlayersInRange() -- scan for players
playersThisScan = filterOnWhitelistedPlayers(whitelist, playersThisScan) -- filter on whitelist
if (shouldDoorOpen()) -- door check
-- tell the door to open, redstone, modem message, whatever
rs.setOutput("back", true)
sleep(20) -- seconds to give the door to open and the player to pass before starting to close
rs.setOutput("back", false")
end
sleep(20) -- seconds to wait before checking for players again
end
Then checking for held item you said the nbt can be important so you need to check what information is available from the sensor, look at targetsDetail.HeldItem table and see what information more than the name that can be used.
Easiest place to have this code is in getPlayersInRange() there most of the information already is available. If it is placed between
playerName = targetsDetail.Username
and
playersFound[playerName] = true
you can make it only sets the value to true if the player have the correct item
I am not able to test this so it will probably not work directly.