Help with creating Fading Music Areas

I am currently trying to create a Music Area script that fades the audio as you enter and leave a part. So far, I’ve got this, it sorta works, but is VERY buggy, such as music not fading when you exit, music not fading in when you enter, music overlapping eachother etc.
(This script is the same in both parts, each has a different audio in the script named “Music”)
local Music = script.Music

Code
local Music = script.Music

local ts = game:GetService("TweenService")

local ti = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)

local tIn = {Volume = 1}

local tOut = {Volume = 0}

local createIn = ts:Create(Music, ti, tIn)

local createOut = ts:Create(Music, ti, tOut)

script.Parent.Touched:Connect(function()

createIn:Play()

end)

script.Parent.TouchEnded:Connect(function()

createOut:Play()

end)

How might I fix this? Is there any other ways I should go about it?

3 Likes

Ive made a few music region scripts in the past and I would NOT recommend using .Touched Signals as they can be very unreliable.

Instead, consider using Region3’s as they are much MUCH easier to work with in these types of situations.

I would personally make a Region3 module to detect when someone enters/leaves a certain region and then handle the music from your main script.

Heres an Example:

local Region3Service = {};

function Region3Service.createRegion(Part)
    --// Convert Part into Region3
    --// Create a custom RBXScriptSignal for the Region3 to detect when it is entered/left
    
    return Signals
end

return Region3Service