Different Areas with Different Musics

The game im making has a background music that plays at all times, however, there is an area around a part that plays it’s own music, I wish to make it so the closer you are to this part the lower the background music becomes so the two dont mash together.

1 Like

Not sure if this is 100% the direction I will go but I had bookmarked this for later review as location based BGM is something I intended to add as well.

Disclaimer: This is NOT my channel.

1 Like

I use this for music zones in my games

2 Likes

You check the distance of the player’s character to every music-emitting part, preferably in a local script, and then adjust volume accordingly. Using the RollOff properties in a sound object, you don’t have to worry about manually playing the music when the player gets close.

--//Local script inside of StarterCharacterScripts.
MusicParts = workspace.MusicParts:GetChildren()
Character = game.Players.LocalPlayer.Character
HRoot = Character.HumanoidRootPart
MinDistance,MaxDistance = 50,75

BackgroundMusic = workspace.BackgroundMusic
BaseBackgroundVolume = 1

while task.wait(1) do --//Should be smooth enough.
	mp_ClosestPart = 99999
	for i,v in ipairs(MusicParts) do
		mp_Magnitude = (ClosestPart.Position - HRoot.Position).Magnitude
		if mp_Magnitude  < ClosestPart then
			mp_ClosestPart = mp_Magnitude

			if mp_Magntiude > MaxDistance then 
				BackgroundMusic.Volume = BaseBackgroundVolume
			else
				mp_MinMax = MaxDistance - MinDistance
				mp_Adjusted = (mp_Magntiude - MinDistance) / mp_MinMax 
				BackgroundMusic.Volume = BaseBackgroundVolume * math.clamp(mp_Adjusted ,0,1)
			end
		end
	end
end

1 Like

Can vouch for Zone+, I use it extensively in my projects

Makes it very easy to detect when players enter/exit an area and you can use multiple parts for complex areas.

1 Like