Issue with my Door System

Hey there! Need some quick support regarding my door system.

So, the way my system is set up, there are multiple folders in the workspace with different door types (Light, Heavy, Gate-class doors). In my handler script, I have multiple tables listing each valid keycard and their corresponding value for each corresponding door type.

The tables containing information on each keycard, their value, and which door type
local lightdoorClearances = {
	['A'] = true;
	['B'] = true;
	['C'] = true
}
local heavydoorClearances = {
	['A'] = false;
	['B'] = true;
	['C'] = true
}
local gateClearances = {
	['A'] = false;
	['B'] = false;
	['C'] = true
}

In another function, I use a “for loop” to check the player’s Backpack for tools + the tool’s name to find any matches between the name of the tool and the values inputted into each dictionary. Then the match is ran through the corresponding clearances table to check if that keycard is set to true or false (the value determines if the keycard has access to open said door type.)

Function that checks for matching tools and their corresponding value
local function validateClearance(player, requestedModel)
	local arrayToCheck = nil
	if requestedModel.Parent.Name == 'LightDoors' then
		arrayToCheck = lightdoorClearances
	elseif requestedModel.Parent.Name == 'HeavyDoors' then
		arrayToCheck = heavydoorClearances
	elseif requestedModel.Parent.Name == 'Gates' then
		arrayToCheck = gateClearances
	end
	for _, child in pairs(player.Backpack:GetChildren()) do
		if child:IsA('Tool') and table.find(validCardNames, child.Name) then
			if arrayToCheck[child.Name] == true then
				return true
			else 
				return false
			end
		else
			return false
		end
	end
end


Video of the system working as intended. This is due to the fact that this door is under the “light door” category and so all keycard values are set to true on the corresponding table.


Video of the system failing. For this door, it’s categorized as a “heavy door”, the corresponding table has keycards named “A” set to false and cards named “B” and “C” set to true.


The problem is that in cases where a table has varying values for each keycard, even if two cards are set to true and one set to false, the result returned back to the code is false when I want it to send back true. So my question is, why does this happen and what’s the fix?

Well, if I’m understanding you correctly, the doors aren’t opening at all even when you have a valid keycard. The problem might be since you created a function name is that validateClearance is never executed

The reason you’re encountering this issue is because the function you’ve created returns false and cancels the function as soon as it finds an invalid one, stopping it from checking the other keycards.

Think of it this way, the system checks keycard ‘a’ and finds that its not allowed to open that door. It then returns false and doesn’t check keycards ‘b’ or ‘c’.

You need to edit your code to be something like this:

local function validateClearance(player, requestedModel)
	local arrayToCheck = nil
	if requestedModel.Parent.Name == 'LightDoors' then
		arrayToCheck = lightdoorClearances
	elseif requestedModel.Parent.Name == 'HeavyDoors' then
		arrayToCheck = heavydoorClearances
	elseif requestedModel.Parent.Name == 'Gates' then
		arrayToCheck = gateClearances
	end
	for _, child in pairs(player.Backpack:GetChildren()) do
		if child:IsA('Tool') and table.find(validCardNames, child.Name) then
			if arrayToCheck[child.Name] == true then
				return true
			end
		end
	end
   return false
end
2 Likes

validateClearance() is ran. With the way the system is set up, everything relies on whatever result is brought back from this function.

Instead of using a loop and considering you are using a hashmap, you can just index the value you know to be true in each door and its usage. That would turn your code from O(n) to O(1), just a simple time complexity problem.

Now, with the code, you are looping through all the tools and returning false if the value is false. As mentioned above, the way to fix this is to use a simple index operation with the keys that you know can open the door rather than all of them.

You could simply rewrite the Hashmap to hold the key name followed by what door they can open, this would fix this problem entirely and is much faster.

This works! But curious, is that “else return false” the problem? Technically speaking, when “else return false” was there, would it have automatically just returned false when the loop was ran on a table with varying values?

I would suggest rewriting the system and basing it on the key names rather than the door names.

Indeed it is.

The issue with what you wrote was that the game is cycling through the player’s inventory checking each item, but if an invalid keycard comes before a valid one, the function returns back false, stopping the correct one(s) from being checked, if that makes sense.

2 Likes

Ah alright, I will consider doing that. Thanks

1 Like

Makes sense, thanks again :slight_smile: :+1:

1 Like