Sound.Ended literally does not work

im trying to make a serverside music player that chooses random songs and doesnt repeat the same one.

this is my script and it doesnt work because sound.ended doesnt work.

local songlist = {
	3626217233,
	6285393445,
	4756558939,
	1167891823,
	830780384,
	14459505053,
	889213758
}

local count = 0
for i,v in pairs(songlist) do
	count = count + 1
end

local song_playing = false
local sound
local Song_Picker = math.random(1,count)
local Previous_Song = Song_Picker

local debounce = false

while wait() do
	if not song_playing then
		song_playing = true
		
		Song_Picker = math.random(1,count)
		while Song_Picker == Previous_Song do
			Song_Picker = math.random(1,count)
		end
	end
	
	if Song_Picker ~= Previous_Song and not debounce then
		debounce = true
		sound = Instance.new("Sound",game.SoundService)
		sound.SoundId = ("rbxassetid://" .. tostring(songlist[Song_Picker]))
		sound.Looped = false
		sound:Play()
	end
end

sound.Ended:Wait()
print("Asdf")
	
song_playing = false
debounce = false
sound:Destroy()
Previous_Song = Song_Picker

i cant use timelength and timeposition because its not replicated.

1 Like

You are in an infinite loop. Once the code inside of the loop is done, the loop will simply repeat. I suggest placing the sound.Ended:Wait() statement inside the loop (at the end).

Edit: I saw the rest of the code in your script, and I think that you should probably also be added to the loop.

2 Likes

Erm, are you aware that a while wait()/true do loop would run infinitely unless you break it?

1 Like

it will never get to the lines below it. So he means put them into the loop

I’m very much aware

local songlist = {
	3626217233,
	6285393445,
	4756558939,
	1167891823,
	830780384,
	14459505053,
	889213758
}

local count = 0
for i,v in pairs(songlist) do
	count = count + 1
end

local song_playing = false
local sound
local Song_Picker = math.random(1,count)
local Previous_Song = Song_Picker

local debounce = false

while wait() do
	if not song_playing then
		song_playing = true
		
		Song_Picker = math.random(1,count)
		while Song_Picker == Previous_Song do
			Song_Picker = math.random(1,count)
		end
	end
	
	if Song_Picker ~= Previous_Song and not debounce then
		debounce = true
		sound = Instance.new("Sound",game.SoundService)
		sound.SoundId = ("rbxassetid://" .. tostring(songlist[Song_Picker]))
		sound.Looped = false
		sound:Play()

        sound.Ended:Wait()
        print("Asdf")
	
        song_playing = false
        debounce = false
        sound:Destroy()
        Previous_Song = Song_Picker
	end
end

3 Likes

This script should work.
I don’t see anything wrong

yeah i realized the sound.ended:Wait() was after the loop but still it doesnt work for some reason. it just stops and it doesnt play the next song after putting it into the loop

I just realized, i was changing the timeposition in workspace but timeposition isnt replicated so its changing it client side, i have to wait until the song actually finishes to see if it plays the song. I’m not sure though if this is true

yeah it works. this was my most embarrassing post tbh.

1 Like