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

How to tell when a song ends, and start a new one?

Right now, I have every song for my game set to looped, but I want it to detect when a song ends and play a different song, unless the song ended because a round started. My script is as follows:

SoundsFolder = game.Workspace.Sounds

local Songs = SoundsFolder.BGM:GetChildren()

print(Songs)

local CurrentSong = Songs[math.random(1,#Songs)]

print(CurrentSong)

CurrentSong:Play()
SoundsFolder.Wind:Stop()

game.ReplicatedStorage.Status.Changed:Connect(function(newstring)
    if newstring == "Start!" then
		SoundsFolder.BGM:Stop()
		SoundsFolder.Wind:Play()
		SoundsFolder.TornadoWarning:Play()
		SoundsFolder.TornadoSiren:Play()
    elseif newstring == "The game has ended! Initializing map cleanup." then
		CurrentSong = Songs[math.random(1,#Songs)]
		SoundsFolder.BGM:Play()
		SoundsFolder.Wind:Stop()
		SoundsFolder.TornadoSiren:Stop()
		SoundsFolder.TornadoWarning:Stop()
    elseif newstring == "Waiting For More Players..." then
		CurrentSong = Songs[math.random(1,#Songs)]
		SoundsFolder.BGM:Play()
		SoundsFolder.Wind:Stop()
		SoundsFolder.TornadoSiren:Play()
		SoundsFolder.TornadoWarning:Stop()
    end
end)

2 answers

You will have to simply use a wait and look at the time position of the sounds, there's no other way that I know of that isn't simple. It's either impossible or I can't do it yet,

I have a model called Make Sound Work that sort of does this, but I don't think it will solve the issue you are finding, it makes it easy to either play multiple songs or only 1 song, change the 1st song volume and if it's looped or not, automatically connects to 5 songs, and auto time position detection, and even allows u to set one of the five songs with a object value connecter.

Link Anyways: Make Music Work [2019]

Here's a bit of the code from my Make Sound Work script that u may want to study.

MusicHelper = script.Music.TimeLength

wait(MusicHelper) -- detects the time length of the song and waits that long then does anything afterward.

4 comments

0

How could I integrate this into my current script?

0

study it, u may want to use the wait line and add that variable to your script so it knows when the song stops.

0

or use if Music.Playing == false then Music2:Play() end

0

in a RunService stepped loop

You can use the Sound.Ended:Wait() to pause the entire script, then when the song ends, the script resumes, here's an example:

local sound = workspace.sound
sound:Play()
sound.Ended:Wait() -- Wait for this specific song to end
print("Song has ended!")

Since, .Ended is an RBXScriptSignal, you can assign a function to it, to execute, (callback), once the song has finished,


sound.Ended:Connect(function()
	print("The sound has ended!")
end)

2 comments

0

Thank you!

0

Accept the answer. :V

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