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.