Is there a way to make music fade in/out?

Hey everyone! Today I wanted to make a fade in/out music script.

Here’s where I got so far:
image

The music doesn’t fade in or out. 100% it’ll only play if I remove the fades. (Just tested it, it actually doesn’t)

Any support/suggestions would be appreciated, thanks!
Reminder: This is in a gui. I basically want to make it where if the frame is visible, the music fades in and starts playing. If the frame isn’t visible, fade out and stop the music.

8 Likes

A few quick questions:

Are you getting any output errors? Are you sure the audio ID in the sound is valid? Have you tried inserting print()s to make sure the code is actually running when you think it is? Does the sound play when clicking the play button in the properties menu?

Also, a much more efficient way to accomplish this is with a loop. It will compress the amount of code needed drastically.

for x = 0,1,0.1 do
    script.Parent.Music.Volume = x
    wait(0.1)
end

The same applies to the reverse:

for x = 1, 0, -0.1 do
    script.Parent.Music.Volume = x
    wait(0.1)
end

This probably won’t fix your problem, but is much more practical.

5 Likes

Weirdly enough, no output errors! And the ID works. I have another script literally just for this, I’m gonna see if it works.
And I’ll try the one you provided.

Also I´m sure that the parameter in :Play() and :Stop() is the fade in and out time

How about a slightly longer wait? May be too quick for the sound to kick in?

Try tweening the volume. TweenService | Documentation - Roblox Creator Hub

8 Likes

You could just do a repeat until for a loop.
Then proceed to do:

var:Connect(f(x)
repeat
sound.volume = sound.volume -0.1
until sound.volume == 0
sound:Stop()
end)
1 Like

I would highly recommend using the link @111mph sent. Tweening will be magnitudes of higher quality than using a loop with wait().

4 Likes

You can get the time the song has been playing so when one sound is nearing its end the second sound can start playing and increase its volume as the first goes down.

for i = 1,10 do --Going down
script.Parent.Music.Volume = script.Parent.Music.Volume = -.1
end

And volume up

for i = 1,10 do --Going up
script.Parent.Music.Volume = script.Parent.Music.Volume = +.1
end
1 Like

For god sake dont use this

--Localization & Editing--
local Music = script.Parent.Music

--End of Editing--

--Code--

while true do --Check
	wait(0.1)
	if
		script.Parent.Visible == false
	then
		script.Parent.Music:Pause() --Pause
		wait(.1)
		script.Parent.Music.Volume = 1
		wait(.1)
		script.Parent.Music.Volume = 0.9
		wait(.1)
		script.Parent.Music.Volume = 0.8
		wait(.1)
		script.Parent.Music.Volume = 0.7
		wait(.1)
		script.Parent.Music.Volume = 0.6
		wait(.1)
		script.Parent.Music.Volume = 0.5
		wait(.1)
		script.Parent.Music.Volume = 0.4
		wait(.1)
		script.Parent.Music.Volume = 0.3
		wait(.1)
		script.Parent.Music.Volume = 0.2
		wait(.1)
		script.Parent.Music.Volume = 0.1
	end
end

while true do --Check
	wait(0.1)
	if
		script.Parent.Visible == true
	then
		script.Parent.Music:Play() --Play
		wait(.1)
		script.Parent.Music.Volume = 0.1
		wait(.1)
		script.Parent.Music.Volume = 0.2
		wait(.1)
		script.Parent.Music.Volume = 0.3
		wait(.1)
		script.Parent.Music.Volume = 0.4
		wait(.1)
		script.Parent.Music.Volume = 0.5
		wait(.1)
		script.Parent.Music.Volume = 0.6
		wait(.1)
		script.Parent.Music.Volume = 0.7
		wait(.1)
		script.Parent.Music.Volume = 0.8
		wait(.1)
		script.Parent.Music.Volume = 0.9
		wait(.1)
		script.Parent.Music.Volume = 1
	end
end

.1 does not slow it down. While true do gives it a slight delay, but it’s only a millisecond or so.
(Keep in mind I configured this for a GUI)

1 Like

One thing I want to say is, that isn’t the best solution for this issue. So you might wanna mark a better solution, so it can help people in the future who might be looking for something similar, also I don’t recommend you to do it that way either.

2 Likes

Alright. I know that isn’t a good way to do it, so I’ll look at other options and see the simplest. But keep in mind, this is a problem for a opening and closing GUI, and most people probably have an answer for that. But most of these could potentially be for something else.

2 Likes

You don’t want to add a loop for this. It might not do much lag alone, but as you add more code to your game it might affect performance.
You see, you can wait or connect events for this, and there is an event called GetPropertyChangedSignal, which fires when a property of the instance changed. You can use:

script.Parent:GetPropertyChangedSignal(“Visible”):connect(function()
-Code Here
end)

It will fire everytime the visible property is changed, instead of looping. You can then check if visible is false to stop the song or true to play it. Also try using tweens, it saves a lot of code sometimes and its smoother, resulting in something like this: Music Fade Gui

This GUI stops/plays the song with a fade effect of 1 second when GUI.Visible is changed.

You can see how tweens work here.

1 Like

Yeah, tweens might be much easier for this type of thing. You should also probably use an event instead of running a loop to continuously check.

local TS = game:GetService("TweenService")
script.Parent:GetPropertyChangedSignal("Visible"):Connect(function()
    if script.Parent.Visible == true then
        script.Parent.Music:Play()
        local Tween = TS:Create(script.Parent.Music, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 1})
        Tween:Play()
    else
        local Tween = TS:Create(script.Parent.Music, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 0})
        Tween:Play()
        Tween.Completed:Wait()
        script.Parent.Music:Stop()
    end
end)
27 Likes

why would you necropost this a year later

1 Like

Thanks you ! You helped me for the song menu of my game !

Don’t use that, use tweens, I’ve learned from experience that their simpler and better

I find it pretty easy to just use a while loop (for loops just aren’t for me :/).
I’d do something like:

local sound = script.parent
local timer = 1
while timer > 0 do
	wait(0.01)
	sound.volume -= 0.01
	timer -= 0.01
end

… Man, really? TWEENS IS THE BEST WAAAAY OH MY GOOOOOOOD, WHY DID YOU NOT USE TWEENS MAN?

1 Like