Hi, i am making a game wich has different sounds. I want the music to fade whenever it starts or ends, i tried putting a localscript inside the sounds with a played and ended function, however when the song ends it does not work. Could anyone show me a good way to script this?
Old script that did not work (Only works when it starts the sound):
script.Parent.Played:Connect(function()
local targetvolume = 0.5
script.Parent.Volume = 0
while wait() do
if script.Parent.Volume <= targetvolume - 0.01 then
script.Parent.Volume = script.Parent.Volume + 0.01
else
if script.Parent.Volume == targetvolume then
break
end
end
end
end)
script.Parent.Ended:Connect(function()
local targetvolume = 0
if script.Parent.Playing == true then
while wait() do
if script.Parent.Volume >= targetvolume + 0.01 then
script.Parent.Volume = script.Parent.Volume - 0.01
else
if script.Parent.Volume == targetvolume then
break
end
end
end
end
end)

1 comment
Thanks!