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

I don’t know why the error invalid argument #1 to ‘pairs’ (table expected, got nil) occurs on page 31.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("(Pet)PlayerExperience")

local function printBudget()
	local budget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync)
	print("Current set/increment budget:", budget)
end

Players.PlayerAdded:Connect(function(player)	
	Instance.new("Folder",player).Name = "Pets"
	Instance.new("Folder",player).Name = "PetsEquiped"

	local key = "Player_" .. player.UserId

	local Pets = player:WaitForChild("Pets")

	for index, descendant in pairs(ReplicatedStorage:FindFirstChild("Pets"):GetChildren()) do
		Instance.new("IntValue",Pets).Name = tostring(descendant)
	end

	wait(1)

	local savedLevel

	local success, err = pcall(function()
		savedLevel = experienceStore:GetAsync(key)
	end)
	if success then
		for i, datas in pairs(savedLevel) do
			local pet = Pets:FindFirstChild(datas[1])
			if pet then
				pet.Value = datas[2]
			else
				local instance = Instance.new(datas[3])
				if instance then
					instance.Name = datas[1]
					instance.Value = datas[2]
					instance.Parent = Pets
				end
			end
		end
	else
		local success, err = pcall(function()
			experienceStore:SetAsync(key, {})
		end)
		if success then
			printBudget()
		else
			warn(err .. "SaveTablePets")
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local key = "Player_" .. player.UserId

	local success, err = pcall(function()
		return experienceStore:GetAsync(key)
	end)

	local Pets = player:FindFirstChild("Pets")
	if not Pets then return end
	Pets = Pets:GetChildren()

	local objData = {}

	for i, obj in pairs(Pets) do
		table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
	end

	if success then
		local success, err = pcall(function()
			experienceStore:SetAsync(key, objData)
		end)
		if success then
			printBudget()
		else
			warn(err .. "SaveTablePets")
		end
	else
		local success, updatedName = pcall(function()
			experienceStore:UpdateAsync(key, function(oldValue)
				oldValue = objData
				return objData
			end)
			if success then
				print("Uppercase Name:", objData)
			end
		end)
	end
end)			

game:BindToClose(function()
	for i, player in pairs(Players:GetPlayers()) do

		local key = "Player_" .. player.UserId

		local success, err = pcall(function()
			return experienceStore:GetAsync(key)
		end)

		local pets = player:FindFirstChild("Pets")
		if not pets then return end
		pets = pets:GetChildren()

		local objData = {}

		for i, obj in pairs(pets) do
			table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
		end

		if success then
			local success, err = pcall(function()
				experienceStore:SetAsync(key, objData)
			end)
			if success then
				printBudget()
			else
				warn(err .. "SaveTablePets")
			end
		else
			local success, updatedName = pcall(function()
				experienceStore:UpdateAsync(key, function(oldValue)
					oldValue = objData
					return objData
				end)
				if success then
					print("Uppercase Name:", objData)
				end
			end)
		end
	end
	wait(3)
end)

It’s a page where errors occur.

	local savedLevel

	local success, err = pcall(function()
		savedLevel = experienceStore:GetAsync(key)
	end)
	if success then
		for i, datas in pairs(savedLevel) do
			local pet = Pets:FindFirstChild(datas[1])
			if pet then
				pet.Value = datas[2]
			else
				local instance = Instance.new(datas[3])
				if instance then
					instance.Name = datas[1]
					instance.Value = datas[2]
					instance.Parent = Pets
				end
			end
		end

The data does not exist chars chars

This means that savedLevel is nil - there is nothing stored in experienceStore"Player_"player's id. You should set a default value for when there is nothing stored so that you don’t get this error

local savedLevel

	local success, err = pcall(function()
		savedLevel = experienceStore:GetAsync(key) or 0
	end)
	if success then
		for i, datas in pairs(savedLevel) do
			local pet = Pets:FindFirstChild(datas[1])
			if pet then
				pet.Value = datas[2]
			else
				local instance = Instance.new(datas[3])
				if instance then
					instance.Name = datas[1]
					instance.Value = datas[2]
					instance.Parent = Pets
				end
			end
		end

The pcall obviously works, because it gets the error. I believe you need to set the value to 0 if there is no data. Something like above should work!

Essentially the same error would occur here; instead of trying to loop through nil it will try to loop through the number 0 which also isn’t a table / dictionary

2 Likes

savedLevel = experienceStore:GetAsync(key) or {}