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