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

Hello everybody! I encountered this problem for the first time “invalid argument #1 to ‘pairs’ (table expected, got nil)”, what’s wrong?

local Dss = game:GetService("DataStoreService"):GetDataStore("TESTNUMBERTHREE")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Tools = Instance.new("Folder")
	Tools.Parent = player
	Tools.Name = "Tools"

	local Sharpening = Instance.new("IntValue")
	Sharpening.Parent = leaderstats
	Sharpening.Name = "Sharpening"
	Sharpening.Value = 0


	local Rebirth = Instance.new("IntValue")
	Rebirth.Parent = leaderstats
	Rebirth.Name = "Rebirth"
	Rebirth.Value = 0

	local OwnedTools = Instance.new("Folder")
	OwnedTools.Parent = Tools
	OwnedTools.Name = "OwnedTools"

	local OwndTool = Instance.new("StringValue")
	OwndTool.Parent = OwnedTools

	local SharpeningUpgrd = Instance.new("StringValue")
	SharpeningUpgrd.Parent = Tools
	SharpeningUpgrd.Name = "SharpeningUpgrd"
	SharpeningUpgrd.Value = "Stone"

	local Money = Instance.new("IntValue")
	Money.Parent = leaderstats
	Money.Name = "Money"
	

	

	local EquipKnife = Instance.new("StringValue", player)
	EquipKnife.Name = "EquipKnife"
	EquipKnife.Value = ""
	
	local OwnedKnife = Instance.new("Folder", player)
	OwnedKnife.Name = "OwnedKnife"
	 
	
	local RebirthMulti= Instance.new("IntValue",leaderstats)
	RebirthMulti.Name = "RebirthMulti"
	RebirthMulti.Value =1
	
game:GetService("ReplicatedStorage"):WaitForChild("Tools"):FindFirstChild("WoodKnife"):Clone().Parent = OwnedKnife
	

	local GetData = Dss:GetAsync("id_" .. player.UserId)

	local OwnedKnifeTable = {}

	if GetData then
		EquipKnife.Value = GetData[5]
		RebirthMulti.Value = GetData[9]
		Rebirth.Value = GetData[10]
		Money.Value = GetData[11]
		Sharpening.Value = GetData[12]
		OwnedKnifeTable  = GetData[13]
	else
		local TableData = {
			EquipKnife.Value,
			RebirthMulti.Value,
			Rebirth.Value ,
			Money.Value,
			Sharpening.Value,
			OwnedKnifeTable   ,
		}
		Dss:GetAsync("id_" .. player.UserId,TableData)
		
	end
	
	for i ,v in pairs(OwnedKnifeTable) do
	
		local NewKnife=  game.ReplicatedStorage.Tools:FindFirstChild(tostring(v)):Clone()
		NewKnife.Parent= OwnedKnifeTable
		
	end

	player.Backpack:ClearAllChildren()
	if EquipKnife.Value ~= "" then
		local KnifeSelect= game.ReplicatedStorage.Tools:FindFirstChild(EquipKnife.Value):Clone()
		KnifeSelect.Parent  = player.Backpack
	end
	
	


game.Players.PlayerRemoving:Connect(function(plr)

	local OwnedKnifeTable = {}

	for i ,v in pairs(plr.OwnedKnife:GetChildren()) do
		if v.Name == "WoodKnife" then 
		else
			table.insert(OwnedKnifeTable,v.Name)
	end
		
	end

	Dss:SetAsync("id_" .. plr.UserId,{
		plr.EquipKnife.Value,
		plr.leaderstats.RebirthMulti.Value,
		plr.leaderstats.Rebirth.Value ,
		plr.leaderstats.Money.Value,
		plr.leaderstats.Sharpening.Value,
		OwnedKnifeTable,
	})
end)

This is exactly the problem "ServerScriptService.leaderstats:154: invalid argument #1 to ‘pairs’ (table expected, got nil) "

If you need any more information to solve the problem, I will provide it

(I will put away the notes now)

is there any data in the table before running the loop?

Since you are getting the OwnedKnifeTable from the datastore, a joined player might not have a value saved for that key, so you will get nil instead of a table that can be iterated. Before running this loop, check if the value of OwnedKnifeTable is nil.

Something like this:

if OwnedKnifeTable then
    --run loop
else
    -- Set the value to an empty table so that the variable can be used later.
    -- It might be better to do this somewhere else in code so that the loop can always run.
    OwnedKnifeTable = {}
end
2 Likes

You set the table as something different.

I am guessing the GetData[13] is nil.

So, I keep reading your script and i’m guessing GetData[13] is suppose to be a table.

I am thinking what happened was you made this DataStore and tested it with different values, then added the OwnedKnifeTable, which caused the DataStore to give you nil as you have no table saved onto your Data.

2 Likes

I went through the tutorial again, but I still can’t understand why this term is used, thanks for the explanation, I’ll try to make a Bool version (that is, I will create a lot of BoolValue in OwnedKnife with the names of knives so that the game can check whether the player has this tool or not)

You were right about zero