Hey everyone! Today I wanted to make a fade in/out music script.
Here’s where I got so far:
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.
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.
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.
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.
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.
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.
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.