local f={}local V={}local X if settings.UseMusicZones==true then for X,n in pairs(zonesfolder:GetChildren())do if n:IsA("\077\111\100\101\108")and n:FindFirstChild("\077\117\115\105\099")then V[X]={}for f,n in pairs(n.Music:GetChildren())do if n:IsA("\083\111\117\110\100")then table.insert(V[X],{SoundId=n.SoundId;Volume=n.Volume,Pitch=n.Pitch})end end f[X]={Priority=n:FindFirstChild("\080\114\105\111\114\105\116\121")and n.Priority.Value or 1;Parts={},Music=V[X]}for V,n in pairs(n:GetChildren())do if n:IsA("\080\097\114\116")then if IsCleanRotation({n.Rotation.X;n.Rotation.Y;n.Rotation.Z})==true then local V,K,A=math.huge,math.huge,math.huge local r,i,q=-math.huge,-math.huge,-math.huge local function z(f)if f.x<V then V=f.x end if f.x>r then r=f.x end if f.y<K then K=f.y end if f.y>i then i=f.y end if f.z<A then A=f.z end if f.z>q then q=f.z end end local x={}local k=n.CFrame local P=n.CFrame*CFrame.Angles(0,math.pi/2,0)local j=n.CFrame*CFrame.Angles(math.pi/2,0,0)local u=k.lookVector*(n.Size.Z/2)local E=P.lookVector*(n.Size.X/2)local F=j.lookVector*(n.Size.Y/2)x[1]=k+u x[2]=k-u x[3]=P+E x[4]=P-E x[5]=j+F x[6]=j-F for f,V in pairs(x)do z(V)end table.insert(f[X].Parts,{Coordinates={lx=V;ly=K,lz=A,mx=r,my=i,mz=q}})else table.insert(f[X].Parts,{Part=n})end end end end end end if settings.UseGlobalBackgroundMusic==true then X=#V+1 V[X]={}for f,n in pairs(globalfolder:GetChildren())do if n:IsA("\083\111\117\110\100")then table.insert(V[X],{SoundId=n.SoundId;Volume=n.Volume;Pitch=n.Pitch})end end end
--now, load the zone & music data into the script

local zones = {}
local music = {}
local globali

if settings.UseMusicZones == true then
	for i, zone in pairs(zonesfolder:GetChildren()) do
		if zone:IsA("Model") and zone:FindFirstChild("Music") then
			music[i] = {}
			for _, sound in pairs(zone.Music:GetChildren()) do --save all of the music data for this zone
				if sound:IsA("Sound") then
					table.insert(
						music[i],
						{ ["SoundId"] = sound.SoundId, ["Volume"] = sound.Volume, ["Pitch"] = sound.Pitch }
					)
				end
			end

			zones[i] = {
				["Priority"] = zone:FindFirstChild("Priority") and zone.Priority.Value or 1,
				["Parts"] = {},
				["Music"] = music[i],
			}

			for _, part in pairs(zone:GetChildren()) do
				if part:IsA("Part") then
					if IsCleanRotation({ part.Rotation.X, part.Rotation.Y, part.Rotation.Z }) == true then --store the min and max X/Y/Z position coordinates of this part
						local lx, ly, lz = math.huge, math.huge, math.huge
						local mx, my, mz = -math.huge, -math.huge, -math.huge
						local function ApplyValues(p)
							if p.x < lx then
								lx = p.x
							end
							if p.x > mx then
								mx = p.x
							end
							if p.y < ly then
								ly = p.y
							end
							if p.y > my then
								my = p.y
							end
							if p.z < lz then
								lz = p.z
							end
							if p.z > mz then
								mz = p.z
							end
						end
						local points = {}
						--a bunch of sloppy math stuff to calculate the coordinates on each of the part's faces so we can figure out the min and max values. mathematicians, pls don't kill me if i'm doing this terribly
						local cf1 = part.CFrame
						local cf2 = (part.CFrame * CFrame.Angles(0, math.pi / 2, 0))
						local cf3 = (part.CFrame * CFrame.Angles(math.pi / 2, 0, 0))
						local lvz = (cf1.lookVector * (part.Size.Z / 2))
						local lvx = (cf2.lookVector * (part.Size.X / 2))
						local lvy = (cf3.lookVector * (part.Size.Y / 2))
						points[1] = cf1 + lvz
						points[2] = cf1 - lvz
						points[3] = cf2 + lvx
						points[4] = cf2 - lvx
						points[5] = cf3 + lvy
						points[6] = cf3 - lvy
						for _, p in pairs(points) do
							ApplyValues(p)
						end
						table.insert(
							zones[i]["Parts"],
							{ ["Coordinates"] = { lx = lx, ly = ly, lz = lz, mx = mx, my = my, mz = mz } }
						)
					else --save a reference to the part itself to be used for GetTouchingParts(), since it has a complex rotation that can't be solved with my own feeble calculations
						table.insert(zones[i]["Parts"], { ["Part"] = part })
					end
				end
			end
		end
	end
end

if settings.UseGlobalBackgroundMusic == true then
	globali = #music + 1
	music[globali] = {}
	for _, sound in pairs(globalfolder:GetChildren()) do
		if sound:IsA("Sound") then
			table.insert(
				music[globali],
				{ ["SoundId"] = sound.SoundId, ["Volume"] = sound.Volume, ["Pitch"] = sound.Pitch }
			)
		end
	end
end
