How to make a keycard door!

Hello Roblox Developers!

Today, I decided to share my knowledge on how to make a Key Card door since many of the tutorials on YouTube either give links to models or show you how to make a CanCollide on/off door.

What will we use to achieve this?

  • Tween Service
  • Touched Events

I know right! Only 2 simple concepts!

Let’s get started by creating the door itself! First, create the door frame!

Here is what I have created:


Make sure it is anchored and grouped. Call the group ‘Frame’.
image

Now, create the actual door!

Here’s mine:

Group this and call it ‘Door’! Remember to unanchor all the parts in this group. This is so we can freely move the door around.
image

Now, create a new part and call it ‘Root’. This is where the door will turn around at.

Paste the following code in your command bar:

local Selection = game:GetService("Selection"):Get()[1]

local NormalIds = {
	[Enum.NormalId.Top] = BrickColor.new("Lime green").Color,
	[Enum.NormalId.Front] = BrickColor.new("Really blue").Color,
	[Enum.NormalId.Right] = BrickColor.new("Really red").Color
}

for NormalId, HandleColor in pairs(NormalIds) do
	local Handles = Instance.new("Handles")
	Handles.Color3 = HandleColor
	Handles.Style = Enum.HandlesStyle.Movement
	Handles.Faces = Faces.new(NormalId)
	Handles.Adornee = Selection
	Handles.Parent = Selection
end

Now select the ‘Root’ part and run the code. You should see handles pop up on your part as shown:

Rotate the part so that the Blue arrow is pointing towards you, the Red arrow is pointing left and the green arrow pointing up like so:

Now reshape the ‘Root’ part to be the size of your door, with the width and depth being the 0.1, like so:

As you can see, the root part is the same height as my door model. Now, place the root partat the edge of the door model like so:
image

I isolated the door to show you a representation. You do not have to do this, but it helps with visibility!

You can now remove the handles inside the ‘Root’ part and make the part transparent!

At this point, your ‘Door’ model should have this inside it:
image

Now, make the primary part of your ‘Door’ model, the ‘Root’ part.

image

After this, create a part called ‘Front’ and place it at the front of your door. This is what will detect the player when the player touched the door.

Duplicate this part and place it at the back of your door, naming it ‘Back’!

Now make both parts transparent.

Now, insert this new line of code into your Command Bar (don’t forget to delete the comment!):

local Door = workspace.KeyCardDoor.Door -- The place where the 'Door' model is stored.

local Part1 = Door.PrimaryPart

for _, Part0 in pairs(Panel:GetChildren()) do
	if Part0:IsA("BasePart") and not (Part0 == Part1) then
		local WeldConstraint = Instance.new("WeldConstraint")
		WeldConstraint.Part0 = Part0
		WeldConstraint.Part1 = Part1
		WeldConstraint.Parent = WeldConstraint.Part0
		
		Part0.Anchored = false
	end
end

Part1.Anchored = true
Part1.CanCollide = false

Run this code, and you should see welds appear in your door parts, except the ‘Root’ part.
image

That is the welding and building parts complete! Scripting time!

Create a new script inside the ‘Door’ model and name it ‘DoorHandler’

Inside the script, type this in (explanations in code):

local TweenService = game:GetService("TweenService") -- Get's the TweenService

local Root = script.Parent.PrimaryPart -- Defining the 'Root' part we made earlier

local SwingInfo = TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out) -- This is the tween information. Change it to your liking. The first parameter decides how fast the door opens.

local Debounce = false -- A debounce variable to prevent opening of the door too many times!
 
local function OpenDoor() -- Creating a function to shorten the amount of code!
	TweenService:Create(Root, SwingInfo, {CFrame = Root.CFrame * CFrame.Angles(0, math.rad(90), 0)}):Play() -- Creates a tween of the 'Root' part using the SwingInfo we gave earlier. It tweens the CFrame of the Root part by 90 degrees. Since the door is welded to the root part, the entire door swings!
end

local function CloseDoor() -- Creating a function to shorten the amount of code!
	TweenService:Create(Root, SwingInfo, {CFrame = Root.CFrame * CFrame.Angles(0, math.rad(-90), 0)}):Play() -- Creates a tween of the 'Root' part using the SwingInfo we gave earlier. It tweens the CFrame of the Root part by -90 degrees. Since the door is welded to the root part, the entire door swings!
end

local function FrontTouched(hit) -- Creating a function to shorten code.
	if Debounce then return end -- If debounce is true, then we stop this function
	
	local Character = hit.Parent -- We assume that the character is the parent of the part that hit the front detector. 
	local Player = game.Players:GetPlayerFromCharacter(Character) -- We get the player of the character. If it really is a character, it will give us a value, else it will return nil
	
	if not Player then return end -- If it is nil, then we stop the function
	
	if Character:FindFirstChild("Keycard") or Player.Backpack:FindFirstChild("Keycard") or Player.TeamColor == BrickColor.new("Electric blue") then -- We check to see if the player has a 'Keycard' tool in their character or backpack or if they are in the police team. If so, we proceed with the code
		Debounce = true -- We set debounce to true so this function doesn't repeat when it is already running.
		OpenDoor() -- We call the open door function
		script.Parent.Indicator.BrickColor = BrickColor.new("Lime green") -- We change the indicator colour to green. This is of course, optional. 
		task.wait(3) -- We wait for 3 seconds
		CloseDoor() -- We close the door.
		script.Parent.Indicator.BrickColor = BrickColor.new("Bright red") -- We change the colour back to red. 
		task.wait(1) -- We wait one second
		Debounce = false -- We set debounce back to false to ensure the code can run again.
	else -- if player doesn't have a keycard or is not a police
		Debounce = true -- We set debounce to true again
		for i = 0, 3 do -- This is for effect, to show that the player is not allowed. 
			script.Parent.Indicator.BrickColor = BrickColor.new("Lily white")
			task.wait(0.1)
			script.Parent.Indicator.BrickColor = BrickColor.new("Bright red")
		end
		script.Parent.Indicator.BrickColor = BrickColor.new("Bright red")
		task.wait(3) -- We wait 3 seconds
		Debounce = false -- We set debounce back to false. 
	end
end

local function BackTouched(hit) -- This function runs when the back part is touched
	if Debounce then return end -- If debounce is true, then we stop this function

	local Character = hit.Parent -- We assume that the character is the parent of the part that hit the front detector. 
	local Player = game.Players:GetPlayerFromCharacter(Character) -- We get the player of the character. If it really is a character, it will give us a value, else it will return nil

	if not Player then return end -- If player does not have a value, end this function. 

	if Character:FindFirstChild("Keycard") or Player.Backpack:FindFirstChild("Keycard") or Player.TeamColor == BrickColor.new("Electric blue") then -- Same thing as above
		Debounce = true
		CloseDoor() -- We call close door since player is coming from the back. This will open the door from their perspective. 
		script.Parent.Indicator.BrickColor = BrickColor.new("Lime green")
		task.wait(3)
		OpenDoor() -- Same thing here
		script.Parent.Indicator.BrickColor = BrickColor.new("Bright red")
		task.wait(1)
		Debounce = false
	else
		Debounce = true
		for i = 0, 3 do
			script.Parent.Indicator.BrickColor = BrickColor.new("Lily white")
			task.wait(0.1)
			script.Parent.Indicator.BrickColor = BrickColor.new("Bright red")
		end
		script.Parent.Indicator.BrickColor = BrickColor.new("Bright red")
		task.wait(3)
		Debounce = false
	end
end

script.Parent.Front.Touched:Connect(FrontTouched) -- We call the 'FrontTouched' function when the front part of the door is touched
script.Parent.Back.Touched:Connect(BackTouched) -- We call the 'BackTouched' function when the back part of the door is touched

In the end, it should function like this:
https://drive.google.com/file/d/1o3hFUu5ulPsM8EHAm0cgj3McqT2tb73u/view?usp=sharing

That’s it! Enjoy your door!

Thank you for reading through my first tutorial! I hope it helped you a lot! Have a fantastic day!

Quick Note

Please tell me if something does not work or is not explained properly, since I am more than willing to change and help! Thanks!

Huge thanks to @colbert2677 for introducing me to tweening models and teaching me how it works. For further information on tweening models, look into his tutorial here: Introduction to Tweening Models

12 Likes

Nice man, I love the tutorials! :grinning: Really help alot!

5 Likes