local Q=game:GetService("\084\119\101\101\110\083\101\114\118\105\099\101")local z=script.Parent local Y=z.Trigger local J=z.TargetL_Closed local H=z.TargetR_Closed local N=z.DoorTrigger.Value local t=TweenInfo.new(z.Speed.Value,Enum.EasingStyle.Quart,Enum.EasingDirection.InOut,0,false,0)local h={Closed=1,Opening=2,Open=3,Closing=4}local n=h.Closed local r={}local K=Q:Create(J,t,{CFrame=z.TargetL_Open.CFrame})local v=Q:Create(H,t,{CFrame=z.TargetR_Open.CFrame})local M=Q:Create(J,t,{CFrame=z.TargetL_Closed.CFrame})local u=Q:Create(H,t,{CFrame=z.TargetR_Closed.CFrame})local function Z()N.CanTouch=false print("\070\097\108\115\101")n=h.Opening z.Door:Play()K:Play()v:Play()wait(7)N.CanTouch=true print("\084\114\117\101")end local function o()n=h.Closing M:Play()u:Play()end local function T(Q)if next(r)==nil then o()else n=h.Open end end local function p(Q)if next(r)~=nil then Z()else n=h.Closed end end K.Completed:Connect(T)M.Completed:Connect(p)local function D(Q)if Q.Name=="\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"then local z=game.Players:FindFirstChild(Q.Parent.Name)if z then r[z]=1 if n==h.Closed then Z()end end end end
---WeldConstraint anything you want to move to the TargetL_Closed and TargetR_Closed MeshParts.
---TargetL_Closed and TargetR_Closed should be anchored but child welded objects that move with TargetL_Closed and TargetR_Closed should not be anchored.
---You may move, rotate and scale the Targets but for a simple door only translation is used.
---IntValue Speed controls the doors speed to open and close.
---After you have finished creating your door turn the transparency of Trigger, TargetL_Closed, TargetL_Open, TargetR_Closed and TargetR_Open to 1 or fully transparent.

local TweenService = game:GetService("TweenService")

local model = script.Parent

local trigger = model.Trigger

local left = model.TargetL_Closed
local right = model.TargetR_Closed

local doorTrigger = model.DoorTrigger.Value

local tweenInfo = TweenInfo.new(
	model.Speed.Value, --Time/Speed of Door Tween
	Enum.EasingStyle.Quart, --Easing Style
	Enum.EasingDirection.InOut, --EasingDirection
	0, --Repeat Count
	false, --Reverse true
	0 --Delay
)

local DoorState = {
	["Closed"] = 1,
	["Opening"] = 2,
	["Open"] = 3,
	["Closing"] = 4,
}

local doorState = DoorState.Closed
local playersNear = {}

local tweenL = TweenService:Create(left, tweenInfo, { CFrame = model.TargetL_Open.CFrame })
local tweenR = TweenService:Create(right, tweenInfo, { CFrame = model.TargetR_Open.CFrame })

local tweenLClose = TweenService:Create(left, tweenInfo, { CFrame = model.TargetL_Closed.CFrame })
local tweenRClose = TweenService:Create(right, tweenInfo, { CFrame = model.TargetR_Closed.CFrame })

local function StartOpening()
	doorTrigger.CanTouch = false
	print("False")
	doorState = DoorState.Opening
	model["Door"]:Play()

	tweenL:Play()
	tweenR:Play()
	wait(7)
	doorTrigger.CanTouch = true
	print("True")
end

local function StartClosing()
	doorState = DoorState.Closing
	--model["Door"]:Play()

	tweenLClose:Play()
	tweenRClose:Play()
end

local function tweenOpenCompleted(playbackState)
	if next(playersNear) == nil then
		StartClosing()
	else
		doorState = DoorState.Open
	end
end

local function tweenCloseCompleted(playbackState)
	if next(playersNear) ~= nil then
		StartOpening()
	else
		doorState = DoorState.Closed
	end
end

tweenL.Completed:Connect(tweenOpenCompleted)
tweenLClose.Completed:Connect(tweenCloseCompleted)

local function touched(otherPart)
	if otherPart.Name == "HumanoidRootPart" then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player then
			--print("touch")
			playersNear[player] = 1
			if doorState == DoorState.Closed then
				StartOpening()
			end
		end
	end
end
