local U={}local I={}module={}function module.AddSeat(s)print("\065\100\100\105\110\103 \115\101\097\116")local Q=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local C=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")table.insert(U,s)script.NewSeat:FireAllClients(s)local n=s.ChildAdded:Connect(function(U)if U.Name=="\083\101\097\116\087\101\108\100"then if U.Part1 then if U.Part1.Name=="\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"then local I=game.Players:GetPlayerFromCharacter(U.Part1.Parent)if I then print("\069\110\116\101\114 \101\118\101\110\116 \102\105\114\101\100")Q:Fire(I)end end end end end)local d=s.ChildRemoved:Connect(function(U)if U.Name=="\083\101\097\116\087\101\108\100"then if U.Part1 then if U.Part1.Name=="\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"then local I=game.Players:GetPlayerFromCharacter(U.Part1.Parent)if I then print("\069\110\116\101\114 \101\118\101\110\116 \102\105\114\101\100")C:Fire(I)end end end end end)s.AncestryChanged:connect(function()if not s:IsDescendantOf(game)then n:Disconnect()d:Disconnect()Q:Destroy()C:Destroy()I[s]=nil for U,I in pairs(U)do if I==s then I=nil end end end end)return Q,C end function module.SeatCharacter(U,I)local s=Instance.new("\087\101\108\100")s.Part0,s.Part1=I,U s.C0,s.C1=CFrame.new(0,.5,0,1,0,0,0,0,1,0,-1,0),CFrame.new(0,I.Size.Y*-0.5+U.Size.Y*-0.5,0,1,0,0,0,0,1,0,-1,0)s.Name="\083\101\097\116\087\101\108\100"s.Parent=I end script.GetSeats.OnServerInvoke=function(I)return U end script.GetInSeat.OnServerEvent:Connect(function(U,I)if not I:FindFirstChild("\083\101\097\116\087\101\108\100")then if U.Character~=nil then local s=U.Character:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if s then local U=(s.Position-I.Position).magnitude if U<=10 then module.SeatCharacter(s,I)end end end end end)

local CurrentSeats = {}
local SeatsInteractingPlayers = {} --Each seat has a table of players containing the last interacting time from each playere

module = {}

function module.AddSeat(Seat)
	print("Adding seat")
	local EnterEvent = Instance.new("BindableEvent")
	local ExitEvent = Instance.new("BindableEvent")
	table.insert(CurrentSeats,Seat)
	script.NewSeat:FireAllClients(Seat)
	
	--Connect seat in listener
	local C1 = Seat.ChildAdded:Connect(function(Obj)
		if Obj.Name == "SeatWeld" then
			if Obj.Part1 then
				if Obj.Part1.Name == "HumanoidRootPart" then
					local Player = game.Players:GetPlayerFromCharacter(Obj.Part1.Parent)
					if Player then
						print("Enter event fired")
						EnterEvent:Fire(Player)
					end
				end
			end
		end
	end)
	
	local C2 = Seat.ChildRemoved:Connect(function(Obj)
		if Obj.Name == "SeatWeld" then
			if Obj.Part1 then
				if Obj.Part1.Name == "HumanoidRootPart" then
					local Player = game.Players:GetPlayerFromCharacter(Obj.Part1.Parent)
					if Player then
						print("Enter event fired")
						ExitEvent:Fire(Player)
					end
				end
			end
		end
	end)
	
	--Clean up after the seat is destroyed
	Seat.AncestryChanged:connect(function()
		if not Seat:IsDescendantOf(game) then
			C1:Disconnect()
			C2:Disconnect()
			EnterEvent:Destroy()
			ExitEvent:Destroy()
			SeatsInteractingPlayers[Seat] = nil 
			for i,v in pairs(CurrentSeats) do
				if v == Seat then
					v = nil
				end
			end
		end
	end)
	
	return EnterEvent,ExitEvent
end

function module.SeatCharacter(HRP,Seat)
	local sw = Instance.new("Weld")
	sw.Part0, sw.Part1 = Seat, HRP
	sw.C0, sw.C1 = CFrame.new(0, 0.5, 0, 1, 0, -0, 0, 0, 1, 0, -1, -0), CFrame.new(0, Seat.Size.Y*-.5+HRP.Size.Y*-.5, 0, 1, 0, -0, 0, 0, 1, 0, -1, -0)
	sw.Name = "SeatWeld"
	sw.Parent = Seat
end

script.GetSeats.OnServerInvoke = function(Player)
	return CurrentSeats	
end

script.GetInSeat.OnServerEvent:Connect(function(Player,Seat)
	if not Seat:FindFirstChild("SeatWeld") then
		if Player.Character ~= nil then
			local HRP = Player.Character:FindFirstChild("HumanoidRootPart")
			if HRP then
				local Dist = (HRP.Position - Seat.Position).magnitude
				if Dist<=10 then
					module.SeatCharacter(HRP,Seat)
				end
			end
		end
	end
end)
