function waitForChild(A,r)while not A:FindFirstChild(r)do A.ChildAdded:wait()end end local A=script.Parent local r=3 local t=CFrame.new()+Vector3.new(0,-1.1,.55)local B=CFrame.new()+Vector3.new(0,-1.1,-0.55)waitForChild(A,"\080\108\097\121\101\114\073\100\084\097\103")waitForChild(A,"\067\111\110\102\105\103\117\114\097\116\105\111\110")waitForChild(A,"\068\111\111\114\049")waitForChild(A,"\068\111\111\114\050")waitForChild(A,"\084\111\117\099\104\068\111\111\114\049")waitForChild(A,"\084\111\117\099\104\068\111\111\114\050")waitForChild(A,"\079\117\116\101\114\069\100\103\101")local h=A.PlayerIdTag.Value local I=A.Configuration local G=A.Door1 local R=A.Door2 local v=A.TouchDoor1 local a=A.TouchDoor2 local H=A.OuterEdge waitForChild(I,"\070\114\105\101\110\100\077\111\100\101")local V=I.FriendMode function testPermission(r)h=A.PlayerIdTag.Value if r==nil then return false end pChar=r.Parent if pChar==nil then return false end pPlay=game.Players:GetPlayerFromCharacter(pChar)if pPlay==nil then return false end if V.Value=="\069\118\101\114\121\111\110\101"then return true elseif V.Value=="\079\110\108\121 \077\101"then if pPlay.userId==h then return true else end elseif V.Value=="\070\114\105\101\110\100\115"then if pPlay:IsFriendsWith(h)then return true else end elseif V.Value=="\066\101\115\116 \070\114\105\101\110\100\115"then if pPlay:IsBestFriendsWith(h)then return true else end elseif V.Value=="\071\114\111\117\112"then if pPlay:IsInGroup(h)then return true else end end return false end local g function doorOpen()g=true weld1=G:FindFirstChild("\068\111\111\114\087\101\108\100")weld2=R:FindFirstChild("\068\111\111\114\087\101\108\100")if not weld1 or not weld2 then return end for A=1,10,1 do weld1.C1=t+Vector3.new(0,0,A*.2)weld2.C1=B+Vector3.new(0,0,A*-0.2)wait(.1)end end function doorClose()weld1=G:FindFirstChild("\068\111\111\114\087\101\108\100")weld2=R:FindFirstChild("\068\111\111\114\087\101\108\100")if not weld1 or not weld2 then return end for A=9,0,-1 do weld1.C1=t+Vector3.new(0,0,A*.2)weld2.C1=B+Vector3.new(0,0,A*-0.2)wait(.1)end g=false end local L=false local T=0 function touchEvent(t)if not t or not t.Parent or t.Parent==A then return end if testPermission(t)then if not L and not g then L=true doorOpen()wait(r)while T>0 do local A=T T=0 wait(A)end doorClose()L=false else T=r end end end function changedEvent(A)if A~="\067\070\114\097\109\101"then return end end changedEvent("\067\070\114\097\109\101")
-- useless comment here

function waitForChild(parent, child)
	while not parent:FindFirstChild(child) do
		parent.ChildAdded:wait()
	end
end

local model = script.Parent
local doorOpenTime = 3

local weld1RelativePosition = CFrame.new() + Vector3.new(0, -1.1, 0.55)
local weld2RelativePosition = CFrame.new() + Vector3.new(0, -1.1, -0.55)

waitForChild(model, "PlayerIdTag")
waitForChild(model, "Configuration")
waitForChild(model, "Door1")
waitForChild(model, "Door2")
waitForChild(model, "TouchDoor1")
waitForChild(model, "TouchDoor2")
waitForChild(model, "OuterEdge")

local doorOwnerId = model.PlayerIdTag.Value -- Update the owner ID
local config = model.Configuration
local doorPart1 = model.Door1
local doorPart2 = model.Door2
local doorTouch1 = model.TouchDoor1
local doorTouch2 = model.TouchDoor2
local outerEdge = model.OuterEdge

waitForChild(config, "FriendMode")
local mode = config.FriendMode

function testPermission(part)
	doorOwnerId = model.PlayerIdTag.Value -- Update the owner ID
	if part == nil then
		return false
	end -- In case part was deleted
	pChar = part.Parent
	if pChar == nil then
		return false
	end
	pPlay = game.Players:GetPlayerFromCharacter(pChar) -- In case player left game
	if pPlay == nil then
		return false
	end

	-- Test permissions
	if mode.Value == "Everyone" then
		return true
	elseif mode.Value == "Only Me" then
		if pPlay.userId == doorOwnerId then
			return true
		else
			-- no access
		end
	elseif mode.Value == "Friends" then
		if pPlay:IsFriendsWith(doorOwnerId) then
			return true
		else
			-- no access
		end
	elseif mode.Value == "Best Friends" then
		if pPlay:IsBestFriendsWith(doorOwnerId) then
			return true
		else
			-- no access
		end
	elseif mode.Value == "Group" then
		if pPlay:IsInGroup(doorOwnerId) then
			return true
		else
			-- no access
		end
	end
	return false
end

local isOpen
function doorOpen()
	isOpen = true
	--if doorPart1:FindFirstChild("DoorWeld") ~= nil then doorPart1.DoorWeld:Remove() end
	--if doorPart2:FindFirstChild("DoorWeld") ~= nil then doorPart2.DoorWeld:Remove() end
	weld1 = doorPart1:FindFirstChild("DoorWeld")
	weld2 = doorPart2:FindFirstChild("DoorWeld")
	if not weld1 or not weld2 then
		return
	end

	-- horrible animation code
	for i = 1, 10 do
		weld1.C1 = weld1RelativePosition + Vector3.new(0, 0, i * 0.2)
		weld2.C1 = weld2RelativePosition + Vector3.new(0, 0, i * -0.2)

		wait(0.1)
	end
end

function doorClose()
	-- horrible animation code
	weld1 = doorPart1:FindFirstChild("DoorWeld")
	weld2 = doorPart2:FindFirstChild("DoorWeld")

	if not weld1 or not weld2 then
		return
	end

	for i = 9, 0, -1 do
		weld1.C1 = weld1RelativePosition + Vector3.new(0, 0, i * 0.2)
		weld2.C1 = weld2RelativePosition + Vector3.new(0, 0, i * -0.2)

		wait(0.1)
	end

	isOpen = false
	--doorPart2.CFrame = doorGyro.cframe -- snap shut through the player, so that it doesn't send them flying
end

local debounce = false
local stayOpenTime = 0
function touchEvent(part)
	if not part or not part.Parent or part.Parent == model then
		return
	end

	--if (part ~= door1 and part ~= door2) then
	if testPermission(part) then
		if not debounce and not isOpen then
			debounce = true
			doorOpen()
			wait(doorOpenTime)
			while stayOpenTime > 0 do
				local tempVariable = stayOpenTime -- allows us to use this variable in the wait while also setting it to zero
				stayOpenTime = 0
				wait(tempVariable)
			end
			doorClose()
			debounce = false
		else
			stayOpenTime = doorOpenTime
		end
	end
	--end
end

function changedEvent(prop)
	-- Only interested in CFrame (position + rotation) changes
	if prop ~= "CFrame" then
		return
	end

	--targetPos = underDoors.Position
	--door1bp.position = side2.Position
	--door2bp.position = side1.Position
	--a = underDoors.CFrame.lookVector
	--lookVector = Vector3.new( math.abs(a.x), math.abs(a.y), math.abs(a.z) )
	--setDoorForces()
end

changedEvent("CFrame") -- Fire once to initialize
