local Y=Effects.new(ChassisModel,EffectsFolder,TopModel)VehicleSeating.SetRemotesFolder(RemotesFolder)VehicleSeating.SetBindableEventsFolder(BindableEventsFolder)local k=nil local z=Chassis.GetDriverSeat()local o=Chassis.GetPassengerSeats()local g={"\082\105\103\104\116\070\111\111\116";"\082\105\103\104\116\076\111\119\101\114\076\101\103","\076\101\102\116\070\111\111\116";"\076\101\102\116\076\111\119\101\114\076\101\103"}local c={"\066\111\100\121\066\097\099\107\065\116\116\097\099\104\109\101\110\116";"\087\097\105\115\116\066\097\099\107\065\116\116\097\099\104\109\101\110\116";"\072\097\116\065\116\116\097\099\104\109\101\110\116"}local function b(Y,k)if Y:IsA("\072\117\109\097\110\111\105\100")then Y=Y.Parent elseif Y:IsA("\080\108\097\121\101\114")then Y=Y.Character end for Y,z in ipairs(Y:GetChildren())do if z:IsA("\065\099\099\111\117\116\114\101\109\101\110\116")then local Y=z:FindFirstChild("\072\097\110\100\108\101")if Y then local z=false for k,o in ipairs(c)do if Y:FindFirstChild(o)then z=true end end if z then Y.Transparency=k end end end end for z,o in ipairs(g)do local g=Y:FindFirstChild(o)if g then g.Transparency=k end end end local function Z(o,g)if o:IsA("\080\108\097\121\101\114")then RemotesFolder.ExitSeat:FireClient(o,false)local Y=o:FindFirstChildOfClass("\080\108\097\121\101\114\071\117\105")if Y then local k=Y:FindFirstChild(UniqueName.."\095\067\108\105\101\110\116\067\111\110\116\114\111\108\115")if k then k:Destroy()end end end b(o,0)if o:IsA("\072\117\109\097\110\111\105\100")then o.Sit=false end if k then k:Disconnect()k=nil end if g==z then z:SetNetworkOwnershipAuto()Chassis.Reset()Y:Disable()end end local function j(o,g)if g and g.Occupant then local Y=true local k=TopModel:GetAttribute("\084\097\107\101\079\102\102\065\099\099\101\115\115\111\114\105\101\115")if k~=nil then Y=k end if Y then b(g.Occupant,1)end end if not o:IsA("\080\108\097\121\101\114")then return end local c=o:FindFirstChildOfClass("\080\108\097\121\101\114\071\117\105")if c then local b=Instance.new("\083\099\114\101\101\110\071\117\105")b.Name=UniqueName.."\095\067\108\105\101\110\116\067\111\110\116\114\111\108\115"b.ResetOnSpawn=true b.Parent=c k=o.CharacterRemoving:Connect(function()Z(o)end)local j=LocalGuiModulePrototype:Clone()j.Parent=b if g==z then local k=DriverScriptPrototype:Clone()k.CarValue.Value=TopModel k.Parent=b k.Disabled=false z:SetNetworkOwner(o)Y:Enable()else local Y=PassengerScriptPrototype:Clone()Y.CarValue.Value=TopModel Y.Parent=b Y.Disabled=false end local r=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")r.Name="\083\099\114\105\112\116\115\082\101\102\101\114\101\110\099\101"r.Value=Scripts r.Parent=b end end

local EffectsInstance = Effects.new(ChassisModel, EffectsFolder, TopModel)

VehicleSeating.SetRemotesFolder(RemotesFolder)
VehicleSeating.SetBindableEventsFolder(BindableEventsFolder)

local CharacterRemovingConnection = nil

local DriverSeat = Chassis.GetDriverSeat()
local AdditionalSeats = Chassis.GetPassengerSeats()

local LEG_PARTS_TO_REMOVE = {"RightFoot", "RightLowerLeg", "LeftFoot", "LeftLowerLeg"}
local ATTACHMENTS_TO_REMOVE = {"BodyBackAttachment", "WaistBackAttachment", "HatAttachment"}

local function setHatsAndLegsTransparency(obj, transparency)
	if obj:IsA("Humanoid") then
		obj = obj.Parent
	elseif obj:IsA("Player") then
		obj = obj.Character
	end

	for _, child in ipairs(obj:GetChildren()) do
		if child:IsA("Accoutrement") then
			local handle = child:FindFirstChild("Handle")
			if handle then
				local shouldRemove = false
				for _, attachmentName in ipairs(ATTACHMENTS_TO_REMOVE) do
					if handle:FindFirstChild(attachmentName) then
						shouldRemove = true
					end
				end

				if shouldRemove then
					handle.Transparency = transparency
				end
			end
		end
	end

	for _, legName in ipairs(LEG_PARTS_TO_REMOVE) do
		local legPart = obj:FindFirstChild(legName)
		if legPart then
			legPart.Transparency = transparency
		end
	end
end

local function onExitSeat(obj, seat)
	if obj:IsA("Player") then
		RemotesFolder.ExitSeat:FireClient(obj, false)

		local playerGui = obj:FindFirstChildOfClass("PlayerGui")
		if playerGui then
			local scriptContainer = playerGui:FindFirstChild(UniqueName .. "_ClientControls")
			if scriptContainer then
				scriptContainer:Destroy()
			end
		end
	end

	setHatsAndLegsTransparency(obj, 0)

	if obj:IsA("Humanoid") then
		obj.Sit = false
	end

	if CharacterRemovingConnection then
		CharacterRemovingConnection:Disconnect()
		CharacterRemovingConnection = nil
	end

	if seat == DriverSeat then
		DriverSeat:SetNetworkOwnershipAuto()
		Chassis.Reset()
		EffectsInstance:Disable()
	end
end

local function onEnterSeat(obj, seat)
	if seat and seat.Occupant then
		local ShouldTakeOffHats = true
		local prop = TopModel:GetAttribute("TakeOffAccessories")

		if prop ~= nil then
			ShouldTakeOffHats = prop
		end

		if ShouldTakeOffHats then
			setHatsAndLegsTransparency(seat.Occupant, 1)
		end
	end

	if not obj:IsA("Player") then
		return
	end

	local playerGui = obj:FindFirstChildOfClass("PlayerGui")
	if playerGui then
		local screenGui = Instance.new("ScreenGui")
		screenGui.Name = UniqueName .. "_ClientControls"
		screenGui.ResetOnSpawn = true
		screenGui.Parent = playerGui

		CharacterRemovingConnection = obj.CharacterRemoving:Connect(function()
			onExitSeat(obj)
		end)

		local localGuiModule = LocalGuiModulePrototype:Clone()
		localGuiModule.Parent = screenGui

		if seat == DriverSeat then
			local driverScript = DriverScriptPrototype:Clone()
			driverScript.CarValue.Value = TopModel
			driverScript.Parent = screenGui
			driverScript.Disabled = false

			DriverSeat:SetNetworkOwner(obj)
			EffectsInstance:Enable()
		else
			local passengerScript = PassengerScriptPrototype:Clone()
			passengerScript.CarValue.Value = TopModel
			passengerScript.Parent = screenGui
			passengerScript.Disabled = false
		end

		local scriptsReference = Instance.new("ObjectValue")
		scriptsReference.Name = "ScriptsReference"
		scriptsReference.Value = Scripts
		scriptsReference.Parent = screenGui
	end
end
