Invalid key to 'next'

it is much easier to understand when you put it that way.

the code does not have any errors, but it still does not work and i don’t know why.

1 Like

Prints are your friend! Can you try adding a print within the if statement like print(song.Name) and also try changing song.Playing = true to song[“Playing”] = true

1 Like

I put a print right here, but nothing showed up.

		if song.Name == songs[CurrentSound] then
			print(song.Name)
			song.Playing = true
			break
1 Like

Could you put a print right before that if statement, so we check if it is looping at all. Just do the same print(song.Name).

1 Like

the loop works and shows all the songs in the list. do not know why the if statement does not work.

1 Like

That is strange. Maybe doing song:Play() instead of song.Playing = true would work?

1 Like

Going back to your original code, this should work
You just need a variable with an index to keep track of which song

local songs = Group:GetChildren()
local SoundIndex = 1
local CurrentSound = songs[SoundIndex]
Skip.MouseButton1Click:Connect(function()
	CurrentSound.Playing = false
	CurrentSound = songs[next(songs,SoundIndex)]
	CurrentSound.Playing = true
end)
1 Like

I might have something wrong in the above code, because im a bit unfamiliar with the next statement, however, this will do what you want, it will move the index until the end and then repeat

local songs = Group:GetChildren()
local SoundIndex = 1
local CurrentSound = songs[SoundIndex]
Skip.MouseButton1Click:Connect(function()
	CurrentSound.Playing = false
	SoundIndex %= #songs + 1
	CurrentSound = songs[SoundIndex]
	CurrentSound.Playing = true
end)
1 Like

what does this line do?

SoundIndex %= #songs + 1

the code worked and the next song played, but the current song never ended.

% is the modulus operator, you can see where someone needed help with it here, and gets a good explaination

But in our case, basically, if you have a list of songs 1 through 10

every time we do index %= 10, our index will stay the same (not counting the +1 we add to it), until it gets to 10, then that line will set it back to 0 (with the +1 starting it at 1) (and the + 1 at the end is to make it keep moving forward)

So basically our index will go 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, etc…

The reason its not stopping is because (and I think someone else mentioned it above) you can’t set the Playing = true and Playing = false, those can only be read from, not written to.

you have to do CurrentSound:Play()
and CurrentSound:Stop()

i did that and it did the same thing.

There might be some other piece of code in your project that is keeping it playing. Not sure, considering in the code you shared, you never show the initial first song being played.

i have checked my code and that part is the only part which is responsible for music playing. would sounds being set to play and loop in studio affect it?

If you mean you are manually setting it in the ‘properties’ panel for the sound, then yes, that might be a problem , because in a running game, usually the properties will not always work with running code.

How and where are you starting the very first sound to play?

the very first sound plays by default from the looped and playing properties in studio.

Not sure then, I would have to make some sounds to try this out.
Sorry I couldnt help more.

Perhaps make a new post with your current problem, “Sound wont stop playing” and maybe someone will know why

Could you try turning it off by default and then using :Play() to start playing it when the player loads in?

already tried that and it just restarted the song