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
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
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?
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.