Invalid argument #1 to 'pairs' (table expected, got nil)

So I made a script that clones a lot of sounds into a folder and now I want to play them but it doesn’t work. Any solutions.

Script:

for i,v in pairs(musicP) do
	print(v)
	if v:IsA(v:GetChildren()) then
		
	v.resume()
	print(v)
		
	end
	
end

I can see multiple errors with your script, and I have a few questions.

  1. What is musicP defined as?

  2. You’re checking if v is a v:GetChildren??

  3. v.resume is undefined, you need to use v:Resume.

1 Like

The argument you’re passing to pairs() is nil.

musicP does not exist.

1 Like

Oh did not even see the v.resume - hah. MusicP = player.leaderstats.Music:GetChildren()

If your sounds are in player.leaderstats.Music, then you can edit your script a bit.

for i,v in pairs(musicP) do
    print(v)
    if v:IsA("Sound") then
        v:Resume() --Or v:Play(), you choose
        print(v)
    end
end

Still saying: [#1 to ‘pairs’ (table expected, got nil)] - AT the first line

Can you show us where you create the “musicP” variable? I assume that it’s not a table.

Here! It may seem a little confusing:

local function musicPlay()
		
		local musicInv = player.leaderstats.Music:GetChildren()
		for i,v in pairs(musicInv) do
	
			if v:IsA("Sound") then
		
			v.Destroy()
		
		end
		end
			
		if level.Value < 10 then 
			
		local musicClone1 = game.ReplicatedStorage.MusicRep.Starter:Clone()
		musicClone1.Parent = player.leaderstats.Music
		
		elseif level.Value >= 10 then 
		
		local musicClone1 = game.ReplicatedStorage.MusicRep.Starter:Clone()
		local musicClone2 = game.ReplicatedStorage.MusicRep.Amateur:Clone()
		musicClone1.Parent = player.leaderstats.Music
		musicClone2.Parent = player.leaderstats.Music
			
		elseif level.Value >= 25 then 
			
		local musicClone1 = game.ReplicatedStorage.MusicRep.Starter:Clone()
		local musicClone2 = game.ReplicatedStorage.MusicRep.Amateur:Clone()
		local musicClone3 = game.ReplicatedStorage.MusicRep.Pro:Clone()
		musicClone1.Parent = player.leaderstats.Music
		musicClone2.Parent = player.leaderstats.Music
		musicClone3.Parent = player.leaderstats.Music
			
		elseif level.Value >= 50 then
			
		local musicClone1 = game.ReplicatedStorage.MusicRep.Starter:Clone()
		local musicClone2 = game.ReplicatedStorage.MusicRep.Amateur:Clone()
		local musicClone3 = game.ReplicatedStorage.MusicRep.Pro:Clone()
		local musicClone4 = game.ReplicatedStorage.MusicRep.Kring:Clone()
		musicClone1.Parent = player.leaderstats.Music
		musicClone2.Parent = player.leaderstats.Music
		musicClone3.Parent = player.leaderstats.Music
		musicClone4.Parent = player.leaderstats.Music
			
		end
	end

Where’s musicP in there? I don’t see a variable called “musicP”, unless I’m just not seeing it.

Look higher up in the chat :smiley: ------------------------

Oh hmm… try changing “MusicP = player.leaderstats.Music:GetChildren()” to:

MusicP = player.leaderstats.Music

EDIT: Actually I’m not sure that’d work. Nevermind that.

That’s where MusicP is defined, not musicP. Programming is case-sensitive.

Looping through an object will not work; what would you be looping through? You have to loop through its children or descendants or whatever else.

4 Likes

Ah you’re right! Man, it’s hard to think I didn’t see that :joy:

Yes, I already edited my post to say nevermind that. I realized the same thing haha.

I get the samme error again :frowning:

@TheDeadTed14, change your variable to this:

musicP = player.leaderstats.Music:GetChildren()

And give @posatta the solution if it works; if you read his reply above, he mentions the misspelling.

Nope it’s not working my script is:

	for i,v in pairs(musicP) do
    print(v)
    if v:IsA("Sound") then
        v:Resume() --Or v:Play(), you choose
        print(v)
    end
end

Dude, just post your whole script so people don’t have to keep going in circles.

3 Likes

You changed the variable name to “musicP”?

Is it still giving the same error, or what’s it doing now?

Yup! It’m still getting an error.

So you changed the variable to this? (note the lowercase m):

musicP = player.leaderstats.Music:GetChildren()

And your for loop is this? (note the lowercase m again):

	for i,v in pairs(musicP) do
    print(v)
    if v:IsA("Sound") then
        v:Resume() --Or v:Play(), you choose
        print(v)
    end
end

If you can, it’d help if you sent the whole script.

And what error is it? Is it th same exact error, or a different error?