function GetLength(o)local S=0 for o,T in pairs(o)do S=S+1 end return S end script.GetInStart.OnServerEvent:Connect(function(o,S)if S:FindFirstChild("\068\111\111\114\072\105\110\103\101")then print("\070\111\117\110\100 \100\111\111\114 \104\105\110\103\101\044 \111\112\101\110\105\110\103 \100\111\111\114")if S.DoorHinge.ClassName~="\072\105\110\103\101\067\111\110\115\116\114\097\105\110\116"then warn("\087\097\114\110\105\110\103\044 \100\111\111\114 \104\105\110\103\101 \105\115 \110\111\116 \097\099\116\117\097\108\108\121 \097 \104\105\110\103\101\033")end if SeatsInteractingPlayers[S]==nil then SeatsInteractingPlayers[S]={}end SeatsInteractingPlayers[S][o]=elapsedTime()S.DoorHinge.TargetAngle=55 wait(3)print("\067\104\101\099\107 \102\111\114 \097\117\116\111\109\097\116\105\099 \099\108\111\115\101")if o.Parent~=nil then print("\080\108\097\121\101\114 \115\116\105\108\108 \104\101\114\101")if SeatsInteractingPlayers[S][o]then print("\084\105\109\101 \115\105\110\099\101 \108\097\115\116 \105\110\116\101\114\097\099\116\105\111\110\058 "..tostring(elapsedTime()-SeatsInteractingPlayers[S][o]))if elapsedTime()-SeatsInteractingPlayers[S][o]>=3 then SeatsInteractingPlayers[S][o]=nil print("\114\101\109\111\118\105\110\103 \112\108\097\121\101\114")end end else SeatsInteractingPlayers[S][o]=nil end print("\078\117\109\098\101\114 \111\102 \105\110\116\101\114\097\099\116\105\110\103 \112\108\097\121\101\114\115\058 "..tostring(GetLength(SeatsInteractingPlayers[S])))if GetLength(SeatsInteractingPlayers[S])==0 then S.DoorHinge.TargetAngle=0 end end end)script.GetInEnd.OnServerEvent:Connect(function(o,S)if S:FindFirstChild("\068\111\111\114\072\105\110\103\101")then if SeatsInteractingPlayers[S]then SeatsInteractingPlayers[S][o]=nil if GetLength(SeatsInteractingPlayers[S])==0 then S.DoorHinge.TargetAngle=0 end end end end)return module

function GetLength(t)
	local l = 0
	for i,v in pairs(t) do
		l=l+1
	end
	return l
end

script.GetInStart.OnServerEvent:Connect(function(Player,Seat)
	if Seat:FindFirstChild("DoorHinge") then --This bit only serves a purpose if there are doors to open/close
		print("Found door hinge, opening door")
		if Seat.DoorHinge.ClassName ~= "HingeConstraint" then warn("Warning, door hinge is not actually a hinge!") end
		if SeatsInteractingPlayers[Seat] == nil then
			SeatsInteractingPlayers[Seat] = {} --This index is removed when the seat is destroyed by the seat add code
		end
		--Record that a player is trying to get in the seat
		SeatsInteractingPlayers[Seat][Player] = elapsedTime()
		--Activate the hinge
		Seat.DoorHinge.TargetAngle = 55
		--If the player doesnt stop trying to get in after 3 seconds, they probably left or it broke.
		wait(3)
		print("Check for automatic close")
		if Player.Parent ~= nil then --Player is still here, check they havent interacted again. 
			print("Player still here")
			if SeatsInteractingPlayers[Seat][Player] then
				print("Time since last interaction: "..tostring(elapsedTime() -  SeatsInteractingPlayers[Seat][Player]))
				if (elapsedTime() - SeatsInteractingPlayers[Seat][Player]) >=3 then --Check they havent started interacting again within 8 seconds
					SeatsInteractingPlayers[Seat][Player] = nil
					print("removing player")
				end
			end
		else
			SeatsInteractingPlayers[Seat][Player] = nil
		end
		print("Number of interacting players: "..tostring(GetLength(SeatsInteractingPlayers[Seat])))
		--Check if the door should be closed
		if GetLength(SeatsInteractingPlayers[Seat]) == 0 then
			Seat.DoorHinge.TargetAngle = 0
		end
	end
end)

script.GetInEnd.OnServerEvent:Connect(function(Player,Seat)
	if Seat:FindFirstChild("DoorHinge") then 
		if SeatsInteractingPlayers[Seat] then
			SeatsInteractingPlayers[Seat][Player] = nil
			--Check nobody is still trying to get in before closing
			if GetLength(SeatsInteractingPlayers[Seat]) == 0 then
				Seat.DoorHinge.TargetAngle = 0
			end
		end
	end
end)


return module
