local function P(P)if RecentAppEnabled==true then local T=false if home:FindFirstChild(P)then if side.RecentApps:FindFirstChild(P)then T=true end side.Status:TweenPosition(UDim2.new(.094,0,.638,0),"\073\110\079\117\116","\081\117\105\110\116",.5,true)side.B1:TweenPosition(UDim2.new(.024,0,.593,0),"\073\110\079\117\116","\081\117\105\110\116",.5,true)for D,c in pairs(side.RecentApps:GetChildren())do if c.Position==UDim2.new(.016,0,.345,0)then if T and side.RecentApps:FindFirstChild(P)then if side.RecentApps[P].Position~=UDim2.new(.015,0,.027,0)then c:TweenPosition(UDim2.new(.032,0,.668,0),"\073\110\079\117\116","\081\117\105\110\116",.25,true)end else c:TweenPosition(UDim2.new(.032,0,.668,0),"\073\110\079\117\116","\081\117\105\110\116",.25,true)end end if c.Position==UDim2.new(.015,0,.027,0)then c:TweenPosition(UDim2.new(.016,0,.345,0),"\073\110\079\117\116","\081\117\105\110\116",.25,true)end if c.Name==P then c:TweenPosition(UDim2.new(.015,0,.027,0),"\073\110\079\117\116","\081\117\105\110\116",.25,true)end if c.Position==UDim2.new(.032,0,.668,0)then if not T then for P=0,1,.2 do wait()c.ImageTransparency=P end c:Destroy()end end end if not T then local T=(home:FindFirstChild(P)):Clone()T.Parent=side.RecentApps T.IconName.Visible=false T.ZIndex=102 T.Size=UDim2.new(.968,0,.358,0)T.ImageTransparency=1 T.Position=UDim2.new(.015,0,.027,0)for P=1,0,-0.2 do wait()T.ImageTransparency=P end end end end end local function T(P,T,D)local c=script.Parent.CarPlay.NowPlaying.MusicTitle local N=script.Parent.CarPlay.NowPlaying.MusicID c.Text=P N.Text=T filter:FireServer("\117\112\100\097\116\101\083\111\110\103",T,D)handler:FireServer("\080\108\097\121\077\117\115\105\099",P,T)nowplaying.Play.ImageRectOffset=Vector2.new(150,0)end local function D(P,T,D)if script.Parent.CarPlay_Data.Playlists:FindFirstChild(T)then local D=script.Parent.CarPlay.NowPlaying.MusicTitle local c=script.Parent.CarPlay.NowPlaying.MusicID local N=script.Parent.CarPlay_Data.Playlists[T]:GetChildren()D.Text=N[P].Name handler:FireServer("\080\108\097\121\077\117\115\105\099",N[P].Name,N[P].Value)c.Text=N[P].Value filter:FireServer("\117\112\100\097\116\101\083\111\110\103",N[P].Value,N[P].Pitch.Value)end end local function c(T)if home:FindFirstChild(T)then handler:FireServer("\076\097\117\110\099\104\065\112\112",T)for P,T in ipairs(home:GetChildren())do T.AppEnabled.Value=false end P(T)home[T].IconName.Visible=false home[T].ZIndex=102 X=home[T].Position.X.Scale Y=home[T].Position.Y.Scale home[T]:TweenSizeAndPosition(UDim2.new(1.599,0,2.591,0),UDim2.new(-0.325,0,-0.965,0),"\079\117\116","\081\117\105\110\116",1.2,true)home[T].Parent=script.Parent.CarPlay.Homescreen for P,D in ipairs(script.Parent.CarPlay:GetChildren())do if D.Name==T then D.Visible=true end end home.Visible=false for P=0,1,.2 do wait()home.Parent[T].ImageTransparency=P end home.Parent.Visible=false end end local function N()for P,T in pairs(home.Parent:GetChildren())do if T.Name=="\077\117\115\105\099"or T.Name=="\077\097\112\115"or T.Name=="\083\101\116\116\105\110\103\115"or T.Name=="\066\077\087"or T.Name=="\078\111\119\080\108\097\121\105\110\103"or T.Name=="\080\111\100\099\097\115\116\115"or T.Name=="\080\104\111\110\101"then handler:FireServer("\076\097\117\110\099\104\072\111\109\101")script.Parent.CarPlay.Music.Library.Visible=true script.Parent.CarPlay.Music.Playlists.Visible=false for P,T in pairs(script.Parent.CarPlay:GetChildren())do if T:IsA("\070\114\097\109\101")and T.Visible==true then T.Visible=false end end;(script.Parent.CarPlay:FindFirstChild(T.Name)).Visible=false home.Parent.Visible=true T:TweenSizeAndPosition(UDim2.new(.23,0,1,0),UDim2.new(X,0,Y,0),"\079\117\116","\081\117\105\110\116",.5,true)home.Visible=true T.ImageTransparency=0 T.ZIndex=2 T.IconName.Visible=true T.Parent=home wait(.4)for P,T in pairs(home:GetChildren())do if T:FindFirstChild("\065\112\112\069\110\097\098\108\101\100")then T.AppEnabled.Value=true end end end end end local function s(D)local c=0 local N=.039 music.Playlists.MusicPlaylist:ClearAllChildren()local s=script.Parent.CarPlay_Data.Playlists[D]music.Playlists.PlaylistName.Text=D for P,T in ipairs(s:GetChildren())do if T:IsA("\073\110\116\086\097\108\117\101")then handler:FireServer("\076\111\097\100\080\108\097\121\108\105\115\116",D,T.Name,T.Value,c)local P=music.Playlists.Template:Clone()P.Name=T.Value P.MusicTitle.Text=T.Name P.MusicID.Text=T.Value P.Position=UDim2.new(.037,0,c,0)P.Parent=music.Playlists.MusicPlaylist P.Pitch.Value=T.Pitch.Value P.Visible=true c=c+N end end for c,N in ipairs(script.Parent.CarPlay.Music.Playlists.MusicPlaylist:GetChildren())do if N:IsA("\084\101\120\116\066\117\116\116\111\110")then N.MouseButton1Down:connect(function()TransitionTo("\080\108\097\121\108\105\115\116\115","\078\111\119\080\108\097\121\105\110\103")P("\078\111\119\080\108\097\121\105\110\103")script.Parent.CarPlay_Data.NowPlayingData.CurrentPlaylist.Value=D script.Parent.CarPlay_Data.NowPlayingData.SongNumber.Value=c T(N.MusicTitle.Text,N.MusicID.Text,N.Pitch.Value)end)end end end local function z()local P=.002 local T=.092 music.Library.LibraryButtons:ClearAllChildren()local D=script.Parent.CarPlay_Data.Playlists for D,c in ipairs(D:GetChildren())do if c:IsA("\070\111\108\100\101\114")then local D=(music.Library:WaitForChild("\084\101\109\112\108\097\116\101")):Clone()D.Parent=music.Library.LibraryButtons D.Name=c.Name D.Visible=true handler:FireServer("\067\114\101\097\116\101\080\108\097\121\108\105\115\116",c.Name,P)D.PlaylistName.Text=c.Name D.Position=UDim2.new(.025,0,P,0)P=P+T end end for P,T in ipairs(script.Parent.CarPlay.Music.Library.LibraryButtons:GetChildren())do if T:IsA("\084\101\120\116\066\117\116\116\111\110")then T.MouseButton1Down:connect(function()TransitionTo("\076\105\098\114\097\114\121","\080\108\097\121\108\105\115\116\115")s(T.Name)end)end end end function TransitionTo(P,T)if debounce==false then debounce=true for D,c in ipairs(script.Parent.CarPlay:GetDescendants())do if c:IsA("\070\114\097\109\101")and c.Name==P then c:TweenPosition(UDim2.new(-1,0,0,0),"\079\117\116","\081\117\105\110\116",.5,true)local P=coroutine.wrap(function()wait(.5)c.Position=UDim2.new(0,0,0,0)c.Visible=false end)P()end if c:IsA("\070\114\097\109\101")and c.Name==T then c.Visible=true c.Position=UDim2.new(1,0,0,0)c:TweenPosition(UDim2.new(0,0,0,0),"\079\117\116","\081\117\105\110\116",.5,true)end handler:FireServer("\084\114\097\110\115\105\116\105\111\110",P,T)debounce=false end end end

local function AddRecentApp(App)
	if RecentAppEnabled == true then
	local sameapp = false
	if home:FindFirstChild(App) then
		if side.RecentApps:FindFirstChild(App) then
			sameapp = true
			end
		side.Status:TweenPosition(UDim2.new(0.094,0,0.638,0), "InOut", "Quint", 0.5, true)
		side.B1:TweenPosition(UDim2.new(0.024,0,0.593,0), "InOut", "Quint", 0.5, true)
		for i, v in pairs(side.RecentApps:GetChildren()) do
			if v.Position == UDim2.new(0.016,0,0.345,0) then
				if sameapp and side.RecentApps:FindFirstChild(App) then
					if side.RecentApps[App].Position ~= UDim2.new(0.015,0,0.027,0) then
							v:TweenPosition(UDim2.new(0.032,0,0.668,0), "InOut", "Quint", 0.25, true)
					end
				else
						v:TweenPosition(UDim2.new(0.032,0,0.668,0), "InOut", "Quint", 0.25, true)
					end
				end
			if v.Position == UDim2.new(0.015,0,0.027,0) then
				v:TweenPosition(UDim2.new(0.016,0,0.345,0), "InOut", "Quint", 0.25, true)
			end
			if v.Name == App then --prevents any same apps to be duplicated ok
				v:TweenPosition(UDim2.new(0.015,0,0.027,0), "InOut", "Quint", 0.25, true)
				end
			
			if v.Position == UDim2.new(0.032,0,0.668,0) then
				if not sameapp then
				for i = 0, 1, 0.2 do
					wait()
					v.ImageTransparency = i
				end
				v:Destroy()
				end
			end
			end
		if not sameapp then
					local app = home:FindFirstChild(App):Clone()
				app.Parent = side.RecentApps
				app.IconName.Visible = false
				app.ZIndex = 102
				app.Size = UDim2.new(0.968, 0, 0.358, 0)
				app.ImageTransparency = 1
				app.Position = UDim2.new(0.015,0,0.027,0)
				for i = 1, 0, -0.2 do
					wait()
					app.ImageTransparency = i
		end
		end
		end
	end
	end

local function PlayMusic(Title, ID, pitch)
	 local musictitle = script.Parent.CarPlay.NowPlaying.MusicTitle
	 local id = script.Parent.CarPlay.NowPlaying.MusicID
		musictitle.Text = Title
		id.Text = ID
		filter:FireServer("updateSong", ID, pitch)
	handler:FireServer("PlayMusic", Title, ID)
		nowplaying.Play.ImageRectOffset = Vector2.new(150, 0)
end

local function PlayMusicUsingSongValue(Val, Playlists, pitch)
	if script.Parent.CarPlay_Data.Playlists:FindFirstChild(Playlists) then
		 local musictitle = script.Parent.CarPlay.NowPlaying.MusicTitle
	 local id = script.Parent.CarPlay.NowPlaying.MusicID
		local playlists = script.Parent.CarPlay_Data.Playlists[Playlists]:GetChildren()
		musictitle.Text = playlists[Val].Name
		handler:FireServer("PlayMusic", playlists[Val].Name, playlists[Val].Value)
		id.Text = playlists[Val].Value
		filter:FireServer("updateSong", playlists[Val].Value, playlists[Val].Pitch.Value)
	end
end


local function LaunchApp(AppToLaunch)
	if home:FindFirstChild(AppToLaunch) then
		handler:FireServer("LaunchApp", AppToLaunch)
		for i, app in ipairs(home:GetChildren()) do
			app.AppEnabled.Value = false -- no jerks that can mess carplay
		end
		AddRecentApp(AppToLaunch)
		home[AppToLaunch].IconName.Visible = false
		home[AppToLaunch].ZIndex = 102
		X = home[AppToLaunch].Position.X.Scale
		Y = home[AppToLaunch].Position.Y.Scale
		home[AppToLaunch]:TweenSizeAndPosition(UDim2.new(1.599, 0,2.591, 0), UDim2.new(-0.325, 0,-0.965, 0), "Out", "Quint", 1.2, true)
		home[AppToLaunch].Parent = script.Parent.CarPlay.Homescreen
		for i, app in ipairs(script.Parent.CarPlay:GetChildren()) do
			if app.Name == AppToLaunch then
				app.Visible = true
			end
		end
		home.Visible = false
		for i = 0, 1, 0.2 do
			wait()
			home.Parent[AppToLaunch].ImageTransparency = i
		end
		home.Parent.Visible = false
end
end

local function LaunchHome()
	for i, v in pairs(home.Parent:GetChildren()) do
		if v.Name == "Music" or v.Name == "Maps" or v.Name == "Settings" or v.Name == "BMW" or v.Name == "NowPlaying" or v.Name == "Podcasts" or v.Name == "Phone" then
			handler:FireServer("LaunchHome")
			script.Parent.CarPlay.Music.Library.Visible = true
			script.Parent.CarPlay.Music.Playlists.Visible = false
			for i, app in pairs(script.Parent.CarPlay:GetChildren()) do
				if app:IsA("Frame") and app.Visible == true then
					app.Visible = false
				end
			end
			script.Parent.CarPlay:FindFirstChild(v.Name).Visible = false
			home.Parent.Visible = true
			v:TweenSizeAndPosition(UDim2.new(0.23,0,1,0), UDim2.new(X,0,Y,0), "Out", "Quint", 0.5, true)
			home.Visible = true
			v.ImageTransparency = 0
			v.ZIndex = 2
			v.IconName.Visible = true
			v.Parent = home
			wait(.4)
				for i, app in pairs(home:GetChildren()) do
					if app:FindFirstChild("AppEnabled") then
			app.AppEnabled.Value = true -- no jerks that can mess carplay
			end
		end
		end
	end
end

local function LoadPlaylist(PlaylistName)
	local Y = 0
	local NextY = 0.039
	music.Playlists.MusicPlaylist:ClearAllChildren()
	local Data = script.Parent.CarPlay_Data.Playlists[PlaylistName]
	music.Playlists.PlaylistName.Text = PlaylistName
	for i, musics in ipairs(Data:GetChildren()) do
		if musics:IsA("IntValue") then
			handler:FireServer("LoadPlaylist",PlaylistName, musics.Name, musics.Value, Y)
			local Template = music.Playlists.Template:Clone()
			Template.Name = musics.Value
			Template.MusicTitle.Text = musics.Name
			Template.MusicID.Text = musics.Value
			Template.Position = UDim2.new(0.037,0,Y,0)
			Template.Parent = music.Playlists.MusicPlaylist
			Template.Pitch.Value = musics.Pitch.Value
			Template.Visible = true
			Y = Y + NextY
		end
	end
		for i, playlists in ipairs(script.Parent.CarPlay.Music.Playlists.MusicPlaylist:GetChildren()) do
if playlists:IsA("TextButton") then
	playlists.MouseButton1Down:connect(function()
		TransitionTo("Playlists", "NowPlaying")
		AddRecentApp("NowPlaying")
		script.Parent.CarPlay_Data.NowPlayingData.CurrentPlaylist.Value = PlaylistName
		script.Parent.CarPlay_Data.NowPlayingData.SongNumber.Value = i
		PlayMusic(playlists.MusicTitle.Text, playlists.MusicID.Text, playlists.Pitch.Value)
	end)
end
end
end

local function CreatePlaylists()
	local Y = 0.002
	local NextY = 0.092
	music.Library.LibraryButtons:ClearAllChildren()
	local Data = script.Parent.CarPlay_Data.Playlists
	for i, playlists in ipairs(Data:GetChildren()) do
		if playlists:IsA("Folder") then
			local Template = music.Library:WaitForChild('Template'):Clone()
			Template.Parent = music.Library.LibraryButtons
			Template.Name = playlists.Name
			Template.Visible = true
			handler:FireServer("CreatePlaylist", playlists.Name, Y)
			Template.PlaylistName.Text = playlists.Name
			Template.Position = UDim2.new(0.025, 0, Y, 0)
			Y = Y + NextY
		end
	end
	for i, playlists in ipairs(script.Parent.CarPlay.Music.Library.LibraryButtons:GetChildren()) do
if playlists:IsA("TextButton") then
	playlists.MouseButton1Down:connect(function()
		TransitionTo("Library", "Playlists")
		LoadPlaylist(playlists.Name)
	end)
end
end
end

function TransitionTo(Prev, Next)
	if debounce == false then
		debounce = true
	for i, frame in ipairs(script.Parent.CarPlay:GetDescendants()) do
		if frame:IsA("Frame") and frame.Name == Prev then
			frame:TweenPosition(UDim2.new(-1,0,0,0), "Out", "Quint", 0.5, true)
			local framediss = coroutine.wrap(function()
				wait(.5)
 		   frame.Position = UDim2.new(0,0,0,0)
			frame.Visible = false
			end)
 			framediss()
		end
		if frame:IsA("Frame") and frame.Name == Next then
			frame.Visible = true
			frame.Position = UDim2.new(1,0,0,0)
			frame:TweenPosition(UDim2.new(0,0,0,0), "Out", "Quint", 0.5, true)
		end
		handler:FireServer("Transition", Prev, Next)
		debounce = false
	end
	end
end
