Detecting player's backpack for a tool in a table not working

Basically, I’m making a keycard door that scans the player’s backpack for a tool, the problem is when I use the proximity prompt, the door does detect it as the player does not have clearance. I am using a table for the different clearences.

Here’s the part of the script that detects the players backpack for the key:

ProximityPrompt.Triggered:Connect(function(player)
	if bool and clearance[player.Backpack:FindFirstChild(clearance) or player.Character:FindFirstChild(clearance) ] then
		bool = false
		script.Parent.AccessGranted:play()
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lime green")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lime green")
		ProximityPrompt.Enabled = false
		openDoor()
		wait(1)
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
		ProximityPrompt.Enabled = true
		bool = true
	elseif bool2 and not clearance[player.Backpack:FindFirstChild(clearance) or player.Character:FindFirstChild(clearance) ] then
		bool2 = false
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Really red")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Really red")
		ProximityPrompt.Enabled = false
		wait(1)
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
		ProximityPrompt.Enabled = true
		bool2 = true
	end
end)

Here’s the table:

local clearance = {
	["Stolen Card"] = true,
	["SD"] = true,
	["REDACTED"] = true,
	["Omni"] = true,
	["MTF"] = true,
	["L5"] = true,
	["L4"] = true,
	["L3"] = true,
	["L2"] = true,
	["L1"] = false
}

And here’s the entire script:

local ProximityPrompt = script.Parent.Parent.KeycardReader1.ProximityPrompt
local door = script.Parent
local bool = true
local bool2 = true
local clearance = {
	["Stolen Card"] = true,
	["SD"] = true,
	["REDACTED"] = true,
	["Omni"] = true,
	["MTF"] = true,
	["L5"] = true,
	["L4"] = true,
	["L3"] = true,
	["L2"] = true,
	["L1"] = false
}

function openDoor()
	script.Parent.DoorOpen:play()
	for i = 1,(door.Size.z / 0.20) do
		door.CFrame = door.CFrame - (door.CFrame.lookVector * 0.20)
		wait()
				end
	door.Transparency = 1
			for i,v in pairs(door:GetChildren()) do
				if v:IsA("Decal") then
					v.Transparency = 1
		end
	end
	wait(3)
	door.Transparency = 0
			for i,v in pairs(door:GetChildren()) do
				if v:IsA("Decal") then
					v.Transparency = 0
				end
			end
	script.Parent.DoorClose:play()
	for i = 1,(door.Size.z / 0.20) do
		wait()
		door.CFrame = door.CFrame + (door.CFrame.lookVector * 0.20)
		end
	end

ProximityPrompt.Triggered:Connect(function(player)
	if bool and clearance[player.Backpack:FindFirstChild(clearance) or player.Character:FindFirstChild(clearance) ] then
		bool = false
		script.Parent.AccessGranted:play()
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lime green")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lime green")
		ProximityPrompt.Enabled = false
		openDoor()
		wait(1)
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
		ProximityPrompt.Enabled = true
		bool = true
	elseif bool2 and not clearance[player.Backpack:FindFirstChild(clearance) or player.Character:FindFirstChild(clearance) ] then
		bool2 = false
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Really red")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Really red")
		ProximityPrompt.Enabled = false
		wait(1)
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
		ProximityPrompt.Enabled = true
		bool2 = true
	end
end)

Is this the line facing the error? if so, use :Play() instead of :play()

Also, you can’t just do :FindFirstChild(clearance), you have to specifically look through the table and see if the index is there. A simple for loop will get the job done.

Alright, so how would I go about doing that?

a simple for loop:

for index, v in pairs(clearance) do
    if player.Backpack:FindFirstChild(index) then
        -- Your code
    end
end

I’d assume I must have formatted something wrong, but I updated my code with yours’s and it still isn’t working.

ProximityPrompt.Triggered:Connect(function(player)
	for index, v in pairs(clearance) do
		if bool and player.Backpack:FindFirstChild(index) then
			bool = false
			script.Parent.AccessGranted:play()
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lime green")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Lime green")
			ProximityPrompt.Enabled = false
			openDoor()
			wait(1)
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
			ProximityPrompt.Enabled = true
			bool = true
		elseif bool2 and not player.Backpack:FindFirstChild(index) then
			bool2 = false
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Really red")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Really red")
			ProximityPrompt.Enabled = false
			wait(1)
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
			ProximityPrompt.Enabled = true
			bool2 = true
		end
	end
end)

Are there any errors occuring? try adding print statements to see if they are being reached.

Well it does sort of work, however when you first open it prints the statement for insufficient clearance (basically not the right card) , but then it opens and is then stuck in a loop of being open and closed.

I believe the problem is that you are using a dictionary instead of an array. What you could do is create an array like this:

local AllowedCards = {"L1", "L2", -- and so on}

Then you are able to do:

for index, v in pairs(AllowedCards) do
    if player.Backpack:FindFirstChild(v) then
        -- Your code
    end
end

Let me know if you got any errors or this way is impractical for you, I personally believe this would be the best way.

My way is correct because there are values associated with each index. Look at L1, it’s set to false so your code will not know that because it is an array.

This is what happened when I used your way, so do you know what I am doing wrong?

Ah, I see. I would still personally use mine, but sorry for that.

Are you referring to the door going invisible randomly? That’s because you set the transparency to 1.

I discovered I am having the same issue with your way as I was with Zayd’s way, I believe the issue is the loop, but I am not for sure.

As shown in the video, when the card is first used it plays the access denied sequence, but then switches to the access granted one and is frozen there for a few seconds before going back to normal.

That’s because it’s still looping through and found that it is in the backpack a bit late. What you could do is the following:

ProximityPrompt.Triggered:Connect(function(player)
local IsInBackpack = false
	for index, v in pairs(clearance) do
		if player.Backpack:FindFirstChild(index) then
          IsInBackpack = true
            end
	end

if IsInBackpack then
			bool = false
			script.Parent.AccessGranted:play()
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lime green")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Lime green")
			ProximityPrompt.Enabled = false
			openDoor()
			wait(1)
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
			ProximityPrompt.Enabled = true
			bool = true
		else
			bool2 = false
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Really red")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Really red")
			ProximityPrompt.Enabled = false
			wait(1)
			script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
			script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
			ProximityPrompt.Enabled = true
			bool2 = true
		end
end)
1 Like