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

So, I’ve been working on something, but when i try to use it i get the error this error:

“error generating data: ServerScriptService.Serializer:57: invalid argument #1 to ‘pairs’ (table expected, got nil) for slot Slot1 for plr GalacticLemon_Aj”

Here is the code:

local data
	local s,x = pcall(function()
		print('pcall')
		print(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name))
		local stuff = {}
		local thing = workspace.Blocks:FindFirstChild(plr.Name)
		if thing then
			for _, t in pairs(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name):GetChildren()) do
				print(t.Name)
				table.insert(stuff,t)
			end
		end
		--local stuff = workspace.Blocks[plr.Name]:GetChildren()
		data = Save.Encrypt(stuff)

I really can’t find any awnsers to this, as most others are un-related

2 Likes

table expected, got nil

Title says it all, I believe workspace.Blocks[plr.Name] doesn’t exist?
Try printing/warning workspace.Blocks[plr.Name] above the loop

1 Like

Screenshot_105
It does seem to exist, which is why I’m so confused.

1 Like

Try using :WaitForChild() instead of just using a dot (for both the plr.Name and blocks)

1 Like

I did that, and it still gives the same error.

1 Like

Have you tried RVCDev’s suggestion?

1 Like

Yeah I have, and it prints the players’ name.

local data
	local data
	local s,x = pcall(function()
		print('pcall')
		print(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name))
		local stuff = {}
		for _, t in pairs(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name)) do
			table.insert(stuff,t)
		end
		--local stuff = workspace.Blocks[plr.Name]:GetChildren()
		data = Save.Encrypt(stuff)
	end)
1 Like

Are you creating the part in another script or is it already physically in the workspace?

edit: maybe try getting children of blocks and printing their names.

Its being created from a script.

Instead of getting the Player’s children, try getting the children of the character (that is, if that’s what you’re trying to do).

I’m trying to get all the children of the players’ created part that all their parts are stored under. Not their characters’ children.

1 Like

When is this code ran?

Maybe you could try setting it as a variable first and if it is nil, skip adding it?

For example:

local thing = workspace.Blocks:FindFirstChild(plr.Name)
local stuff = {}
if thing then
	for _, t in pairs(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name)) do
		table.insert(stuff,t)
	end
2 Likes

This code is ran when the saveEvent is fired, which I know works well, because I have a print statement in there. I changed the code to look like this which still gives the same error. Also, when I print the names of what’s getting detected, it shows everything.

local data
	local s,x = pcall(function()
		print('pcall')
		print(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name))
		local stuff = {}
		local thing = workspace.Blocks:FindFirstChild(plr.Name)
		if thing then
			for _, t in pairs(workspace:WaitForChild('Blocks'):WaitForChild(plr.Name):GetChildren()) do
				print(t.Name)
				table.insert(stuff,t)
			end
		end
		--local stuff = workspace.Blocks[plr.Name]:GetChildren()
		data = Save.Encrypt(stuff)
	end)
	
	local su,xa = pcall(function()
		local key = tostring(plr.UserId..'&'..(slot))
		DS:SetAsync(key,data)
	end)

And the error is:
“error generating data: ServerScriptService.Serializer:57: invalid argument #1 to ‘pairs’ (table expected, got nil) for slot Slot1 for plr GalacticLemon_Aj”

I know its not the module, because I have made some other things with the same module that work perfectly.

Why are you even doing the loop in the first place? You commented out a simpler way to get the same result right below it:

--local stuff = workspace.Blocks[plr.Name]:GetChildren()

The pairs loop visible in your provided code looks fine, :GetChildren() never returns nil, only ever an empty array. If workspace.Blocks[plr.Name] didn’t exist, then you’d get an error about that instead. The problem probably lies within the Save.Encrypt(stuff) function on this line:

data = Save.Encrypt(stuff)
1 Like

True, but the line still give the same error. It’s probably not that line, because it only needs one argument and that’s the objects in a table.

1 Like

So we know it isn’t the player name or the table itself, then perhaps there is nothing in the there to retrieve which would also result in a nil value?
Maybe try:

local thing = workspace.Blocks:FindFirstChild(plr.Name)
		if thing then
            local thingtable = thing:GetChildren()
            if thingtable then
		        data = Save.Encrypt(thingtable)
            end
        end
	end)
	
	local su,xa = pcall(function()
		local key = tostring(plr.UserId..'&'..(slot))
		DS:SetAsync(key,data)
	end)
2 Likes

Can we see the code for the Save.Encrypt() function?

Also, which line in particular is giving the error? (i.e. which line is line 57?)

^
Also, give specific errors (just copy the error and paste it here) and show any other errors if you have them. If you already replied with those, edit your original message and put the full error there so the future helpers can see it.

2 Likes

Okay, I can do that in the morning when i get back on my computer.

1 Like

I did give the specific error, but I can update the main post with it in the morning.

1 Like