i want to use a script to fade the music in and out but adding small amount of volume to make it seem like its fading in doesn't seem to work?, anyone able to help me out Thanks! ```i = 15 -- intermission timesssssssssssssssssssssssssss music:Play() partyf.Enabled = true partys.Enabled = true partyf2.Enabled = true partys2.Enabled = true partyf3.Enabled = true partys3.Enabled = true while i>= 0 do stat.Value = ('Game Begining in ' .. i) wait(1.15) -- timer i = i -1 end
This can be accomplished by a simple for loop
.
Wiki definition: A loop is a chunk of code that is executed multiple times. There are three types of loops: for, while, and repeat. Each type loops a block of code, but in different ways.
For this example, we will be using the for loop
.
Since you want the volume to fade in, we'll want the for loop
to be increasing.
There are 3 values needed here, your starting point, end point, and how much you wish to increase/decrease the starting point by, within a specific time frame.
for i = 0, 0.5, 0.1 do
This essentially means that i
is equal to 0
, and it wants to be equal to 0.5
(the default volume of a Roblox sound instance), and that it wishes to increase by an increment of 0.1
.
Now, the code inside of this loop will repeat until the end point has been reached, and it will then break
.
So we should set the volume of the sound equal to the value of i
.
music.Volume = i
Now, we have a minor problem, this code will run too fast for anybody to even notice, and this is because there is no delay between each loop, meaning that it thinks we want it to just run the code as fast as possible, however this is not the case.
We can fix this by adding a wait()
and setting a value for how long we wish to wit between each loop, after we set the volume to i
.
wait(0.1)
This means that every 0.1
seconds, the volume will increase by 0.1
until it reaches 0.5
, at which point the loop will break
.
Now, you can mess with these values a little yourself, to find a fade that you think fits your needs.
You can read more on basic loops here.
Full code:
for i = 0, 0.5, 0.1 do music.Volume = i wait(0.1) end
If you have any queries or concerns, then leave a comment and I'll get back to you as soon as possible.
Happy scripting!
Just make a for loop that decreases the volume gradually.