01 | while true do |
02 | game:GetService( "RunService" ).Heartbeat:Wait() |
03 | if (sound.TimePosition > = 73.8 ) and ( not fade) then |
04 | fade = true |
05 | for i = 0 , 1 , - 0.1 do |
06 | sound.Volume = i |
07 | wait( 0.5 ) |
08 | end |
09 | sound.TimePosition = 0 |
10 | for i = 0 , 1 , 0.1 do |
11 | sound.Volume = i |
12 | wait( 0.5 ) |
13 | end |
14 | fade = false |
15 | end |
16 | end |
sound.TimePosition = 0
is being ran before the for
loop finishes, causing the audio to abruptly jump back to the beginning instead of slowly fading out, then going back to the beginning after the fade finishes. I can confirm that this is the issue as I have another script printing the audio's TimePosition
every heartbeat. No other scripts are affecting the audio.
It's probably because you're stepping with -0.1 even though your goal is 1 in the first loop. As well as this, checking the time can be pretty hacky. I'd recommend using the Sound.Ended event.
In addition to using an infinite loop, i think it might be better if you just set the Loop property of the sound object to true, then use the Ended and Played event instead.
01 | Sound.Looped = true |
02 |
03 | Sound.Played:Connect( function () |
04 | --fade in |
05 | for i = 0 , 1 , 0.1 do |
06 | Sound.Volume = i |
07 | wait( 0.5 ) |
08 | end |
09 | end ) |
10 |
11 | Sound.Ended:Connect( function () |
12 | --fade out |
13 | for i = 1 , 0 , - 0.1 do |
14 | Sound.Volume = i |
15 | wait( 0.5 ) |
16 | end |
17 | end ) |
Hopefully this helps.