Invalid key to 'next'

This topic has been made before, but it does not seem to apply to my case, or I do not understand.

I am trying to make a music playlist which skips when you press a button. the songs are sound objects inside a sound group.

this is the code responsible for skipping

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

what is the issue?

2 Likes

Can you try rewriting your code like this?

local songs = {}
for i, song in pairs(Group:GetChildren()) do
   table.insert (songs, song)
end

local CurrentSound = next(songs, nil)
Skip.MouseButton1Click:Connect(function()
	CurrentSound.Playing = false
	next(songs,CurrentSound).Playing = true
end)
1 Like

hi,

currentSound is a sound, not an index, thats the issue

Only used it once but as I remember next returns index and value, so you are using it wrong. And i think next does not go to start (index 1), if there is no next indeces

1 Like

how do i get the index then?

characterblahblahblahq

1 Like

i don’t need any spoonfeeding right now. can i just know what’s wrong and how this snippet fixes it?

1 Like

My apologies. I’m not sure if it will work, that’s why I asked you to try it. The error seems to be that by doing CurrentSound = songs[1], the variable CurrentSound doesn’t equal to the index of the song[1] but rather the value of the table entry with the index[1]. You get the index of the first table entry by doing next(tableName, nil). Then, you can use next over and over to move down the table and cycle all of the songs.

1 Like

the next() function is recognised now, but the properties cannot be set on the sound because the index is only a number. How do i get the corresponding instance from the index to change the properties?

1 Like

Perhaps you could loop through all of the sounds until the value (name) of the currentSound is equal to the name of the sound from the soundgroup, and then set .Playing to be true.

local songs = {}
for i, song in pairs(Group:GetChildren()) do
   table.insert (songs, song)
end

local CurrentSound = next(songs, nil)
Skip.MouseButton1Click:Connect(function()
    for i, song in pairs(Group:GetChildren()) do
        if song.Name == songs[CurrentSound]) then
           song.Playing = true
           break
        end
    end
end)

I apologize for “spoonfeeding” you the code but it is much easier to explain this way, you do not have to use it and again, I am not sure if it works. Feel free to ask me questions about any part of the code and I’ll gladly explain it to the best of my ability.

1 Like

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()