Music Area with fade in/fade out

Making sounds play in specific areas can be very useful for biom ambience, or simply an area/room with music. This setup uses parts that have can collide off, can touch off, and can query on, and the others are optional, but 1 transparency is recommended. The other thing you need is a folder in sound service called music. Every part with those properties is going to be named the same as an audio inside the Music folder. So a part that will play “Song” that is in the Music folder will be named “Song”. The parts can be put in any parent within Workspace, or Workspace itself. The last thing needed is the script. This script is a Local script inside StarterCharacter.

script.Parent:WaitForChild("PartInTheCharacter")

local MusicPlayed

game:GetService("RunService").RenderStepped:Connect(function()
	local Parts = game.Workspace:GetPartsInPart(script.Parent.PartInTheCharacter)
	for _, Part in pairs(Parts) do
		if game.SoundService.Music:FindFirstChild(Part.Name) then
			if game.SoundService.Music:FindFirstChild(Part.Name).Volume == 0 then
				for i = 1,100 do
					game.SoundService.Music:FindFirstChild(Part.Name).Volume += 0.01 --This should be one hundredth of the volume of the music you want.
					task.wait()
				end
				game.SoundService.Music:FindFirstChild(Part.Name).Volume = 1 --This should be the volume of the music you want
				MusicPlayed = game.SoundService.Music:FindFirstChild(Part.Name)
				break
			end
		end
		if MusicPlayed ~= nil then
			local CanFadeOut = true
			for _, Part in pairs(Parts) do
				if Part.Name == MusicPlayed.Name then
					CanFadeOut = false
					break
				end
			end
			if CanFadeOut == true then
				if MusicPlayed.Volume == 1 then --"1" Should be the volume of the music you want
					local MusicToStop = MusicPlayed
					for i = 1,100 do
						MusicToStop.Volume -= 0.01 -- This should be one hundredth of the volume of the music you want
						task.wait()
					end
					MusicToStop.Volume = 0
				end
			end
		end
	end
end)

All Task.Wait()'s can be any wait time, depending on how long you want the fade in/fade out to be.

4 Likes

Hi,

do you have a demo editable place to check out with different biomes in fade outs?

Thanks

TestPlace.rbxl (59.3 KB)

Here is the file. By the way, the music area parts should be anchored, or else they would fall through the map (you probably already knew that) and the music should be 0 volume, with looped and playing enabled.

1 Like

cool, thanks.

  1. does looped even with 0 volume and playing, mean it is still playing and using resources, even tho you do not here it?

  2. is the sound parented to a part, and then played client side only?

The sound is in a folder named “Music” If you want, all of the parts of the script that set the volume to what you want (like I said in the script preview) you can add wether they are playing or not. For the one that sets it to 0, it makes it not playing. For the one the sets it to not 0, before the loop that fades it in, you can make it play. For my own uses I am only using 15 audios, which will not be too laggy, but if you need to implement that you can.

umm, ya… is the sound client side localscript?

The script is a local script. Just look what I did inside the test place.