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