Sound.Ended:Wait() does not work

i’m trying to make it so when the sound ends, it activates the function playsound() again, so music can loop. But by using the property Sound.Ended:Wait() it seems like it does nothing because after testing, it doesn’t print anything or play the other track.

local rp = game:GetService("ReplicatedStorage")

local MusicFolder = rp:FindFirstChild("Music")

print("Loaded")

local Musics = MusicFolder:GetChildren()

function playmusic()

   local playone = math.random(1,#Musics)

   print(playone)

   MusicFolder[playone]:Play()

   MusicFolder[playone].Ended:Wait()

   print("ended")

   playmusic()

end

playmusic()
1 Like

Try using the duration of the audio itself in a wait so that it will play after the audio ends.

4 Likes

From the API article, there might be some stuff you’ll have to check:

This event will not fire for sounds with Sound.Looped set to true as they continue playing upon reaching their end.

It will also not fire when the sound is stopped before playback has completed, for this use Sound.Stopped.

^Another script could be stopping the audio, idk.

2 Likes

I would do that, but the game i’m making contains super powers like Freezing Time, in which the songs that are playing, stop. Therefore i want the whole track to play and not end abruptly.

1 Like

I checked the API article beforehand, and:

*The sound is only being played by the script.
*The sound is not Looped
*The sound stops, but then resumes once time has resumed (read the previous response).

Could you try using Sound.Stopped instead of Sound.Ended?

2 Likes

Will that still trigger if the sound is Paused instead of Stopped?

1 Like

If you want it to trigger if it’s paused you can use Sound.Paused, otherwise if the sound is done playing you would use Sound.Stopped

1 Like

I’ll try using the Sound.Stopped for now, i’ll report back in a couple of minutes to see if the issue was resolved.

1 Like

If that doesn’t work, right after you play the audio you can wait music.TimeLength

1 Like

See the first reply on why i can’t use wait()

1 Like

It didn’t work, here’s how i did it:

local rp = game:GetService("ReplicatedStorage")
local MusicFolder = rp:FindFirstChild("Music")
print("opa bohne")
local Musics = MusicFolder:GetChildren()

function playmusic()
	local playone = math.random(1,#Musics)
	print(playone)
	MusicFolder[playone]:Play()
	MusicFolder[playone].Stopped:Connect(function()
		print("Music has ended.")
		playmusic()
	end)
end

playmusic()
1 Like

Do this:
MusicFolder[playone]:Play() Wait(MusicFolder[playone].TimeLength) playmusic()

2 Likes

I’m telling you, i can’t do this because players can freeze time ingame which also pauses the music, which after time has resumes it plays the music again, then causing it to end abruptly.

1 Like

If they control time in game, make the script get a remote event, and then the remote event waits however long it lasts for then plays it again / unpauses it.

1 Like

The problem doesn’t occur when it stops, or resumes time by itself, it occurs that even when Time is stopped the wait() is still on course, say the player stops time for 15 seconds, and the song is 30 seconds long, then it will only play 15 seconds of the song. Do you understand now?

1 Like

Well either way, i guess i will use your solution. Since if nothing else works i guess that’s just the way i have to go…

1 Like

Correct me if I’m wrong, but don’t you have to use :connect after an event? That may be your problem.

1 Like

I did. (30 character limitation)

1 Like

:connect had been deprecated due to inconsistency. it has been replaced by :Connect

1 Like