Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make the music fade in/out when starts/ends?

Asked by 8 years ago

I am trying to make a lobby in game music and i am trying to figure out how to make the sound fade in when new song and when fade out towards the end of the song for 6 songs that plays right after each other. Here is what i have so far:

while true do wait() 

    game.Workspace.S1:Play() 
    wait(125) 
    game.Workspace.S1:Stop()

    game.Workspace.S2:Play() 
    wait(125) 
    game.Workspace.S2:Stop()

    game.Workspace.S3:Play() 
    wait(125) 
    game.Workspace.S3:Stop()

    game.Workspace.S4:Play() 
    wait(125) 
    game.Workspace.S4:Stop()

    game.Workspace.S5:Play() 
    wait(125) 
    game.Workspace.S5:Stop()

    game.Workspace.S6:Play() 
    wait(125) 
    game.Workspace.S6:Stop()

end

This is just a script in workspace with the 6 sound holders in workspace as well. Please help! Thanks!

1 answer

Log in to vote
3
Answered by 8 years ago
local ValidIds = {366306298} -- Enter your sound ids here

function PlaySong() -- Function
    local Sound = Instance.new("Sound",game.Workspace) -- Create a new Sound
    Sound.Name = "LobbyMusic" -- Name the Sound
    Sound.SoundId = "http://www.roblox.com/asset/?id=" .. ValidIds[math.random(1,#ValidIds)] -- Pick a random sound from the table(ValidIds)
    Sound.Volume = 0 -- Change the Volume to 0(For fading in)
    Sound.Pitch = 1 -- Change the pitch, default is 0.5
    wait(1) -- Wait 1 second to play it
    Sound:Play() -- Play the song
    for i = 1,50 do -- Fade in
        wait(0.1) -- If this isn't here it'll be instant
        Sound.Volume = Sound.Volume + 0.05 -- Change the volume
    end -- End the for i = 1,50
    repeat wait() until Sound.TimeLength > 10 -- Cause it won't work properly without this, wait til the song length is above 10 seconds(you can change this if needed)
    wait(Sound.TimeLength - 1) -- Wait til the song is over - 1 second
    for i = 1,50 do -- Fade out
        wait(0.1) -- If this isn't here it'll be instant
        Sound.Volume = Sound.Volume - 0.05 -- Change the volume
    end -- End the for i - 1,50
    Sound:Stop() -- Stop the song(in-case it didn't stop yet)
    Sound:Destroy() -- Deletes the song
end -- End the function

PlaySong() -- Play a song

while wait(125) do -- Loop
    PlaySong() -- Play a song forever :D
end -- End the loop

If you do not want to use my full script, the only actual part you need for fading in is:

for i = 1,50 do
    wait(0.1)
    Sound.Volume = Sound.Volume + 0.05
end

Same for fading out:

for i = 1,50 do
    wait(0.1)
    Sound.Volume = Sound.Volume - 0.05
end

If you have any questions, comment on the answer and I'll try to help you.

0
its kinda late but plz explain me how does "for i = 1,50 do" work ROLANBLOX 4 — 4y
0
not gonna lie ROLANBLOX but this post is 3 years old. i'll help ya though. so it repeats the code block fifty times because it goes through 1 to 50. don't forget the end at the end NoobsWithNoobs 0 — 4y
0
NoobsWithNoobs wow it all makes sense now dude thanks for commenting this your epic! ItsMagmaCream -5 — 4y
Ad

Answer this question