-- Luke



local model = script.Parent
local cache = {}
local closeTime = 0


local OPEN_TIME = 3


function openDoor()
	local currentTime = tick()
	if currentTime > closeTime then	-- This means the door is currently closed
		closeTime = currentTime + OPEN_TIME

		-- Open
		--todo: play sound
		for part,transparency in pairs(cache) do
			if part then
				part.Transparency = 1
			end
		end

		-- Wait
		while closeTime > tick() do
			wait(closeTime-tick())
		end

		-- Close
		--todo: play sound
		for part,transparency in pairs(cache) do
			if part and part.Parent then
				part.Transparency = transparency
			end
		end

	else
		-- If currently open, the hold door open for longer
		closeTime = currentTime + OPEN_TIME
	end
end

function touched(hit)
	if hit and hit.Parent then
		local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
		if humanoid and humanoid.Health > 0 then
			openDoor()
		end
	end
end

for _,part in pairs(model:GetChildren()) do
	if part:IsA('BasePart') then
		part.Touched:connect(touched)
		cache[part] = part.Transparency
	end
end
