How to I make this door auto close behind the player?

I’m making an SCP game. I have this door that can be opened with a keycard, but I’d like that the door closes behind the player so anything can’t escape. How can I do this?

! I’m not a scripter so I don’t know everything about scripting !
The only thing I wish to achieve is that the door closes after sometime after opening.

Here’s the Script

local debounce = false
local open = false
local clearance = {
	['[SCP] Card-L0'] = false,
	['[SCP] Card-L1'] = false,
	['[SCP] Card-L2'] = false,
	['[SCP] Card-L3'] = true,
	['[SCP] Card-L4'] = true,
	['[SCP] Card-L5'] = true,
	['[SCP] Card-Omni'] = true
}
local ButtonKeycard1 = script.Parent.ScriptedComponents.ButtonKeycard1
local ButtonKeycard2 = script.Parent.ScriptedComponents.ButtonKeycard2
local Door1 = script.Parent.ScriptedComponents.Door1
local Door2 = script.Parent.ScriptedComponents.Door2

function onTouched(Hit)
	if not debounce then
		debounce = true
		if not open and Hit.Name == 'Handle' and clearance[Hit.Parent.Name] then
			Door1.AccessGranted:Play()
			Door1.DoorOpen:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(0.10, 0, 0)
				wait()
			end
			open = true
		elseif Hit.Name == 'Handle' and clearance[Hit.Parent.Name] then
			Door1.AccessGranted:Play()
			Door1.DoorClose:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(-0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(-0.10, 0, 0)
				wait()
			end
			open = false
		elseif Hit.Name == 'Handle' and not clearance[Hit.Parent.Name] then
			Door1.AccessDenied:Play()
		end
		wait(0.4)
		debounce = false
	end
end

ButtonKeycard1.Touched:connect(onTouched)
ButtonKeycard2.Touched:connect(onTouched)

If any more info is required then I will happily provide it!

You could use Region3, TouchEnded or ZonePlus

1 Like

You could use task.wait(x), so after x amount of time the door closes.

Maybe you could add an invisible part infront of the door, and when the player touches the part, the door slams shut

I tried this but the door opened infinitely

local debounce = false
local open = false
local clearance = {
	['[SCP] Card-L0'] = false,
	['[SCP] Card-L1'] = false,
	['[SCP] Card-L2'] = false,
	['[SCP] Card-L3'] = true,
	['[SCP] Card-L4'] = true,
	['[SCP] Card-L5'] = true,
	['[SCP] Card-Omni'] = true
}
local ButtonKeycard1 = script.Parent.ScriptedComponents.ButtonKeycard1
local ButtonKeycard2 = script.Parent.ScriptedComponents.ButtonKeycard2
local Door1 = script.Parent.ScriptedComponents.Door1
local Door2 = script.Parent.ScriptedComponents.Door2

function onTouched(Hit)
	if not debounce then
		debounce = true
		if not open and Hit.Name == 'Handle' and clearance[Hit.Parent.Name] then
			Door1.AccessGranted:Play()
			Door1.DoorOpen:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(0.10, 0, 0)
open = true
				wait(5)
Door1.DoorClose:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(-0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(-0.10, 0, 0)
				wait()
			end
			open = false
			end
			
		elseif Hit.Name == 'Handle' and clearance[Hit.Parent.Name] then
			Door1.AccessGranted:Play()
			Door1.DoorClose:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(-0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(-0.10, 0, 0)
				wait()
			end
			open = false
		elseif Hit.Name == 'Handle' and not clearance[Hit.Parent.Name] then
			Door1.AccessDenied:Play()
		end
		wait(0.4)
		debounce = false
	end
end

ButtonKeycard1.Touched:connect(onTouched)
ButtonKeycard2.Touched:connect(onTouched)

Use a task.wait() to make it close after.

Where should I put it exactly? I’m not a scripter.

local debounce = false
local open = false
local clearance = {
	['[SCP] Card-L0'] = false,
	['[SCP] Card-L1'] = false,
	['[SCP] Card-L2'] = false,
	['[SCP] Card-L3'] = true,
	['[SCP] Card-L4'] = true,
	['[SCP] Card-L5'] = true,
	['[SCP] Card-Omni'] = true
}
local ButtonKeycard1 = script.Parent.ScriptedComponents.ButtonKeycard1
local ButtonKeycard2 = script.Parent.ScriptedComponents.ButtonKeycard2
local Door1 = script.Parent.ScriptedComponents.Door1
local Door2 = script.Parent.ScriptedComponents.Door2

function onTouched(Hit)
	if not debounce then
		debounce = true
		if not open and Hit.Name == 'Handle' and clearance[Hit.Parent.Name] then
			Door1.AccessGranted:Play()
			Door1.DoorOpen:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(0.10, 0, 0)
				wait()
			end
			open = true
		elseif Hit.Name == 'Handle' and clearance[Hit.Parent.Name] then
			Door1.AccessGranted:Play()
			Door1.DoorClose:Play()
			for i = 1, 34 do
				Door1.CFrame = Door1.CFrame * CFrame.new(-0.10, 0, 0)
				Door2.CFrame = Door2.CFrame * CFrame.new(-0.10, 0, 0)
				wait()
			end
			open = false
		elseif Hit.Name == 'Handle' and not clearance[Hit.Parent.Name] then
			Door1.AccessDenied:Play()
		end
		wait(0.4)
		debounce = false
	end
end

ButtonKeycard1.Touched:connect(onTouched)
ButtonKeycard2.Touched:connect(onTouched)

Like that but I dont really find how to do, Im not the best at this sorry

1 Like

I just found another similar door. Took its Script and Modified it a bit. Now works as intended.

local Debounce = false
local Open = false
local clearance = {
	['[SCP] Card-L0'] = false,
	['[SCP] Card-L1'] = false,
	['[SCP] Card-L2'] = false,
	['[SCP] Card-L3'] = true,
	['[SCP] Card-L4'] = true,
	['[SCP] Card-L5'] = true,
	['[SCP] Card-Omni'] = true
}
local bool = true
local bool2 = true
local AutoClose = true -- [BM] Set to false if you dont want it to autoclose
local WaitToClose = (3) -- [BM] Time until it auto closes

local KeycardReader1 = script.Parent.ScriptedComponents.ButtonKeycard1
local KeycardReader2 = script.Parent.ScriptedComponents.ButtonKeycard2
local LeftDoor = script.Parent.ScriptedComponents.Door1
local RightDoor = script.Parent.ScriptedComponents.Door2
local OpenSound = LeftDoor.DoorOpen
local CloseSound = LeftDoor.DoorClose
local GrantSound = LeftDoor.AccessGranted
local DeniedSound = LeftDoor.AccessDenied

function openDoor()
	if not Debounce then
		Debounce = true
		if Open then
			Open = false
			CloseSound:Play()
			spawn(function()
				for i = 1, 34 do
					LeftDoor.CFrame = LeftDoor.CFrame * CFrame.new(0.10, 0, 0)
					wait(0.05)
				end
			end)
			spawn(function()
				for i = 1, 34 do
					RightDoor.CFrame = RightDoor.CFrame * CFrame.new(0.10, 0, 0)
					wait(0.05)
				end
			end)
		else
			Open = true
			OpenSound:Play()
			spawn(function()
				for i = 1, 34 do
					LeftDoor.CFrame = LeftDoor.CFrame * CFrame.new(-0.10, 0, 0)
					wait(0.05)
				end
			end)
			spawn(function()
				for i = 1, 34 do
					RightDoor.CFrame = RightDoor.CFrame * CFrame.new(-0.10, 0, 0)
					wait(0.05)
				end
			end)
		end
		wait(2.5)
		Debounce = false
	end
end

KeycardReader1.touched:connect(function(touch)
	if touch.Name == "Handle" and clearance[touch.Parent.Name] and bool and AutoClose == true then
		bool = false
		GrantSound:Play()
		wait(0.75)
		openDoor()
		wait(3)
		bool = true
		wait(WaitToClose)
		bool = false
		wait(0.75)
		openDoor()
		wait(3)
		bool = true
	elseif touch.Name == "Handle" and clearance[touch.Parent.Name] and bool and AutoClose == false then
		bool = false
		GrantSound:Play()
		wait(0.75)
		openDoor()
		wait(3)
		bool = true
	elseif touch.Name == "Handle" and not clearance[touch.Parent.Name] and bool2 then
		bool2 = false
		DeniedSound:Play()
		wait(1)
		bool2 = true
	end
end)

KeycardReader2.touched:connect(function(touch)
	if touch.Name == "Handle" and clearance[touch.Parent.Name] and bool and AutoClose == true then
		bool = false
		GrantSound:Play()
		wait(0.75)
		openDoor()
		wait(3)
		bool = true
		wait(WaitToClose)
		bool = false
		wait(0.75)
		openDoor()
		wait(3)
		bool = true
	elseif touch.Name == "Handle" and clearance[touch.Parent.Name] and bool and AutoClose == false then
		GrantSound:Play()
		wait(0.75)
		openDoor()
		wait(3)
		bool = true
	end
end)

1 Like

Thats amazing! Well done and wish you luck!