local E={}local R={}module={}function module.AddSeat(S)print("\065\100\100\105\110\103 \115\101\097\116")local l=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local g=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")table.insert(E,S)script.NewSeat:FireAllClients(S)local j=S.ChildAdded:Connect(function(E)if E.Name=="\083\101\097\116\087\101\108\100"then if E.Part1 then if E.Part1.Name=="\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"then local R=game.Players:GetPlayerFromCharacter(E.Part1.Parent)if R then print("\069\110\116\101\114 \101\118\101\110\116 \102\105\114\101\100")l:Fire(R)end end end end end)local r=S.ChildRemoved:Connect(function(E)if E.Name=="\083\101\097\116\087\101\108\100"then if E.Part1 then if E.Part1.Name=="\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"then local R=game.Players:GetPlayerFromCharacter(E.Part1.Parent)if R then print("\069\110\116\101\114 \101\118\101\110\116 \102\105\114\101\100")g:Fire(R)end end end end end)S.AncestryChanged:connect(function()if not S:IsDescendantOf(game)then j:Disconnect()r:Disconnect()l:Destroy()g:Destroy()R[S]=nil for E,R in pairs(E)do if R==S then R=nil end end end end)return l,g end function module.SeatCharacter(E,R)local S=Instance.new("\087\101\108\100")S.Part0,S.Part1=R,E S.C0,S.C1=CFrame.new(0,.5,0,1,0,0,0,0,1,0,-1,0),CFrame.new(0,R.Size.Y*-0.5+E.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=R end script.GetSeats.OnServerInvoke=function(R)return E end script.GetInSeat.OnServerEvent:Connect(function(E,R)if not R:FindFirstChild("\083\101\097\116\087\101\108\100")then if E.Character~=nil then local S=E.Character:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if S then local E=(S.Position-R.Position).magnitude if E<=10 then module.SeatCharacter(S,R)end end end end end)
--To Do:
-- Sanity Checks
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 * -0.5 + HRP.Size.Y * -0.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)
