Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a sound fade out to play another sound?

I'm trying to make a sound fade out, and another sound fade in. What should I use to make this happen?

if game.Workspace:FindFirstChild("Sound") == nil then
    local Ambient = Instance.new("Sound")
    Ambient.Parent = game.workspace
    Ambient.SoundId = "rbxassetid://238895410"
    Ambient:Play()
    Ambient.Volume = 1
    end
end

if game.Workspace:FindFirstChild("Sound") == nil then
	local FloweredRust = Instance.new("Sound")
	FloweredRust.Parent = game.workspace 
	FloweredRust.SoundId = "rbxassetid://180919842"
	FloweredRust:Play()
	FloweredRust.Volume = 1
	end

2 answers

Use a for loop. For example:

local setTime = 10
local Sound = Instance.new('Sound', game.Workspace)
Sound.SoundId = 'rbxassetid://238895410'
Sound.Volume = 1
Sound:Play()
wait(setTime)
--Fadeout function below
for i = 1, (Sound.Volume/0.1) do
	wait()
	Sound.Volume = Sound.Volume - 0.1
end
--Fade-in function below
for i = 1, Sound.Volume/0.1 do
	wait()
	Sound.Volume = Sound.Volume + 0.1
end

Hope this helps :) If there's a problem just say something. I could have forgotten a thing or two.

I fixed some errors in your script and added a function that will fade out the first sound passed to it, and then fade in the second. The third variable is the end volume of song2, the song fading in.

if not game.Workspace:FindFirstChild("Ambient") then
    Ambient = Instance.new("Sound")
    Ambient.Name = "Ambient"
    Ambient.Parent = game.workspace
    Ambient.SoundId = "rbxassetid://238895410"
    Ambient.Volume = 1
end

if not game.Workspace:FindFirstChild("FloweredRust") then
    FloweredRust = Instance.new("Sound")
    FloweredRust.Name = "FloweredRust"
    FloweredRust.Parent = game.workspace 
    FloweredRust.SoundId = "rbxassetid://180919842"
	FloweredRust.Volume = 1
end

local function fade(song1,song2, vol)
	for i = song1.Volume, 0, -0.01 do --Fade out song1
		wait(0.1)
		song1.Volume = i
	end
	song1:Stop()

	for i = song2.Volume, vol, 0.01 do --Fade in song2
		wait(0.1)
		song2.Volume = i
	end
	song2.Volume = vol
	song2:Play()
end

FloweredRust:Play()
wait(15)
fade(FloweredRust,Ambient,1)

This discussion is preserved as it was. New answers, comments, and votes are closed.