function waitForChild(e,X)while not e:FindFirstChild(X)do e.ChildAdded:wait()end end local e=script.Parent local X=3 local y=CFrame.new()+Vector3.new(0,-1.1,.55)local j=CFrame.new()+Vector3.new(0,-1.1,-0.55)waitForChild(e,"\080\108\097\121\101\114\073\100\084\097\103")waitForChild(e,"\067\111\110\102\105\103\117\114\097\116\105\111\110")waitForChild(e,"\068\111\111\114\049")waitForChild(e,"\068\111\111\114\050")waitForChild(e,"\084\111\117\099\104\068\111\111\114\049")waitForChild(e,"\084\111\117\099\104\068\111\111\114\050")waitForChild(e,"\079\117\116\101\114\069\100\103\101")local Z=e.PlayerIdTag.Value local B=e.Configuration local P=e.Door1 local S=e.Door2 local U=e.TouchDoor1 local g=e.TouchDoor2 local t=e.OuterEdge waitForChild(B,"\070\114\105\101\110\100\077\111\100\101")local V=B.FriendMode function testPermission(X)Z=e.PlayerIdTag.Value if X==nil then return false end pChar=X.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==Z then return true else end elseif V.Value=="\070\114\105\101\110\100\115"then if pPlay:IsFriendsWith(Z)then return true else end elseif V.Value=="\066\101\115\116 \070\114\105\101\110\100\115"then if pPlay:IsBestFriendsWith(Z)then return true else end elseif V.Value=="\071\114\111\117\112"then if pPlay:IsInGroup(Z)then return true else end end return false end local N function doorOpen()N=true weld1=P:FindFirstChild("\068\111\111\114\087\101\108\100")weld2=S:FindFirstChild("\068\111\111\114\087\101\108\100")if not weld1 or not weld2 then return end for e=1,10,1 do weld1.C1=y+Vector3.new(0,0,e*.2)weld2.C1=j+Vector3.new(0,0,e*-0.2)wait(.1)end end function doorClose()weld1=P:FindFirstChild("\068\111\111\114\087\101\108\100")weld2=S:FindFirstChild("\068\111\111\114\087\101\108\100")if not weld1 or not weld2 then return end for e=9,0,-1 do weld1.C1=y+Vector3.new(0,0,e*.2)weld2.C1=j+Vector3.new(0,0,e*-0.2)wait(.1)end N=false end local l=false local h=0 function touchEvent(y)if not y or not y.Parent or y.Parent==e then return end if testPermission(y)then if not l and not N then l=true doorOpen()wait(X)while h>0 do local e=h h=0 wait(e)end doorClose()l=false else h=X end end end function changedEvent(e)if e~="\067\070\114\097\109\101"then return end end changedEvent("\067\070\114\097\109\101")


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, .55)
local weld2RelativePosition = CFrame.new() + Vector3.new(0, -1.1, -.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 * .2)
		weld2.C1 = weld2RelativePosition + Vector3.new(0, 0, i * -.2)

		wait(.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 * .2)
		weld2.C1 = weld2RelativePosition + Vector3.new(0, 0, i * -.2)

		wait(.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
