Keycard Reader

Learn to have multiple keycards accepted or denied!

by 9kilo99

Author Avatar

Hello there, my name is Eandvwigle. Scrolling through the tutorial page, I found that there was only 1 keycard tutorial. I can not say for sure whether it works or not because I have not run it, but based on what I read, this is a different approach to the keycard door.

This will include:

The first step is to make a table. For this tutorial, I am going SCP themed, but you can make it whatever you want.

local authorizedcards = {
["Level-0"] = false, --Set to false for not allowed.
["Level-1"] = false, --A comma should be used for making new lines
["Level-2"] = false,
["Level-3"] = true, --Set to true if this card is authorized
["Level-4"] = true,
["Level-5"] = true --The last line does not need to have a comma.
}

Now that you have your dictionary of cards and their authorization values, you are now ready to form the functional part of your script. This following function will check to see if the passed value matches any of the cards, and return true or false.

local function checkauthorization(cardname)
    for name, value in pairs(authorizedcards) do --start a for loop to go through the table
        if tostring(cardname) == tostring(name) then -- If the string of the cardname is equal to the string of the name of the card stored in the dictionary (confusing way to put it, I know) then it will continue.
            if value == true then -- If it's authorized, then continue.
                return true --Return the function with the boolean value of "true."
            end
        end
    end
    return false -- if the for loop completes itself, and does not return, then the function will return false.
end

Yay! The function is now in place to check if the card is authorized. Now it's time to make the function that will do the rest of the work! We are going to imagine that this script is inside of the keycard sensor.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.ClassName == "Tool" then --Because the thing touching the sensor will be the tool's handle, we need to find the parents ClassName
        local isauthorized = checkauthorization(tostring(hit.Parent.Name)) --For this, I am using the tool's name, but it can be what you want.
        if isauthorized == true then
            --Write your code to open the door here
        elseif isauthorized == false then
            --Write your code to handle unauthorized cards here
        else
            --This means something is wrong and the function failed to return a boolean value.
            error("Something happened.  The checkauthorization function failed to return a bool value!")
        end
    end
end)

And that's that. You just learned how to authorize multiple cards to a single door. Awesome! Alright, that's it for me. Have a nice day.

View in-game to comment, award, and more!