Sound Fading Function Not Working

So I am making a weather cycle and when it rains it switches to a different audio. I have a function which is supposed to fade out the audio, stop it, change the id, play the audio, and then fade it back in. Because otherwise, it is very sudden. But, it is very glitchy and only one audio appears to play. I’m probably just missing something obvious but oh well.

function changeAudio(sound, newId, endVolume)

	for i = tonumber(sound.Volume), 0, -0.1 do
		sound.Volume = i
		wait(0.1)
	end
	
	sound:Stop()
	sound.SoundId = newId
	sound:Play()
	
	for i = 0, endVolume, 0.1 do
		sound.Volume = i
		wait(0.1)
	end
	
end

--Weather Cylce
spawn(function()
	while wait() do
		
		wait(math.random(5, 5) --[[* 60]])
		
		isRaining = true
		
		particles1.Enabled = true
		particles2.Enabled = true
		
		changeAudio(backgroundMusic, rainingBackgroundMusic, 0.2)
		
		wait(math.random(5,5) --[[* 60]])
		
		particles1.Enabled = false
		particles2.Enabled = false
		
		rainSound:Stop()
		changeAudio(backgroundMusic, dayBackgroundMusic, 0.06)
		
	end
end)

So I tried changing it to this:

function fadeAudio(inout, audio, endVolume)
	if inout == "in" then
		for i = tonumber(audio.Volume), endVolume, 0.1 do
			audio.Volume = i
			wait(0.1)
		end
	else
		for i = endVolume, 0, -0.1 do
			audio.Volume = i
			wait(0.1)
		end
	end
end

function changeAudio(sound, newId, endVolume)

	fadeAudio("out", sound, endVolume)
	
	sound:Stop()
	sound.SoundId = newId
	sound:Play()
	
	fadeAudio("in", sound, endVolume)
	
end

--Weather Cylce
spawn(function()
	while wait() do
		
		wait(math.random(5, 5) --[[* 60]])
		
		isRaining = true
		
		particles1.Enabled = true
		particles2.Enabled = true
		
		changeAudio(backgroundMusic, rainingBackgroundMusic, 0.2)
		
		wait(math.random(5,5) --[[* 60]])
		
		particles1.Enabled = false
		particles2.Enabled = false
		
		rainSound:Stop()
		changeAudio(backgroundMusic, dayBackgroundMusic, 0.06)
		
	end
end)

Now, it is playing the audio in the correct way except it is not fading in and out. It is just cutting immediately. :thinking:

Wait, never mind I somehow got it working… XD

Would you mind sharing how you got this function to work? This is a pretty vague conclusion to the thread and doesn’t help anyone with potentially the same issue figure out what it is that you did to resolve the issue.

3 Likes

I think when I had used changeAudio and I was inputting the rainingBackgroundMusic the volume I was inputting was 0.2 and I used that in the fadeAudio function which was setting the volume at the start of the song a lot louder than normal. That fixed part of it but I am not sure what the main problem was. I was really tired that day but, here is the finished code.

function fadeAudio(inout, audio, endVolume)
	if inout == "in" then
		for i = tonumber(audio.Volume), endVolume, 0.1 do
			audio.Volume = i
			wait(0.5)
		end
	else
		for i = endVolume, 0, -0.1 do
			audio.Volume = i
			wait(0.5)
		end
	end
end

function changeAudio(sound, newId, endVolume)

	fadeAudio("out", sound, 0)
	
	sound:Stop()
	sound.SoundId = newId
	sound:Play()
	
	fadeAudio("in", sound, endVolume)
	
end

--Weather Cylce
spawn(function()
	while wait() do
		
		wait(math.random(5, 5) * 60)
		
		isRaining = true
		
		particles1.Enabled = true
		particles2.Enabled = true
		
		changeAudio(backgroundMusic, rainingBackgroundMusic, 0.2)
		
		wait(math.random(1,2) * 60)
		
		particles1.Enabled = false
		particles2.Enabled = false
		
		rainSound:Stop()
		changeAudio(backgroundMusic, dayBackgroundMusic, 0.06)
		backgroundMusic:Play()
		
	end
end)

1 Like