Why isn't this music fade in/out script working?

Hello, I have this script:

workspace.MapInfo.Winner.Changed:Connect(function()
    if workspace.MapInfo.Winner.Value == localPlayer.Name then
        game.SoundService.Sound.Playing = false
        game.SoundService.victorytheme.Playing = true
        local sound = game.SoundService.victorytheme
        local volume = 0

        while volume < 1 do
            sound.Volume = volume
            volume = volume + 0.1
            wait(0.1)
        end

        while volume > 0 do
            sound.Volume = volume
            volume = volume - 0.1
            wait(0.1)
        end
    else
        game.SoundService.Sound.Playing = true
        game.SoundService.victorytheme.Playing = false
    end
end)

but the music doesn’t play at all or the duration for the effect is too short. How would I fix this? Would implementing sound tween service be a better option? How would I do it?

Thanks in advance.

1 Like

Rather than using the .Playing property, you should instead use :Play()

For example:
Old code:

game.SoundService.Sound.Playing = true

New code:

game.SoundService.Sound:Play()
2 Likes

Thanks, I’ll try that out and let you know

1 Like

It does work that way, but the original music named “Sound” still plays when victorytheme is also playing and the duration of the song is too short

2 Likes

What exactly do you mean by the duration being too short? Does it fade out too quick?

2 Likes

Yes, it does fade out too quickly

2 Likes

Perhaps you could try and mess around with the wait points and negating the volume, maybe try change their values to fit your interests?

1 Like

I fixed it fading out too quickly by setting this:

		while volume > 0 do
			sound.Volume = volume
			volume = volume - 0.1
			wait(10)

but the fade out effect does not happen

1 Like

Are you sure that isn’t because it waits for 10 seconds? It might just be taking a lot of time to fade.

Also, how long is the audio that you are using?

2 Likes

The audio I’m using is 22 seconds long

1 Like

In that case, the audio wouldn’t have enough time to fade and the volume would only decrease by around 0.2.

You may need to decrease the wait value so that it is still able finish the fade before the audio ends, you might also need to add a check for where the sound currently is by using the TimePosition property.

The wait would also have to be below 2.2 seconds (If you aren’t wanting to do anything with the TimePositions)

1 Like

For this, you could maybe do something like:

while volume > 0 and sound.TimePosition == 11 do -- Checks if volume is above 0, and if TimePosition is halfway through. (You can change when you want it to begin fading)
			sound.Volume = volume
			volume = volume - 0.2 -- Decreases volume twice as fast. 
			wait(2) -- Waits for 2 seconds each time (You can also change this to match with the TimePosition)

Apologies if this code doesn’t work, I’ve been feeling kind of tired today

2 Likes

yes, tweening would be better

local ts = game:GetService('TweenService')
local sound = yoursound

ts:Create(sound,TweenInfo.new(1),{Volume = 0}):Play() -- fade out

ts:Create(sound,TweenInfo.new(1),{Volume = 1}):Play() -- fade in
1 Like