Hello fellow devforum users! I have recently made a datastore script to save players pets, but the output keeps returning with “Attempt to index nil with ‘EquippedPets’” and I can’t seem to fix it. Please help.
this is a server script in ServerScriptService:
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("PetDataStore")
local RS = game:GetService("ReplicatedStorage")
local petsFolder = RS:WaitForChild("Pets")
local doSaving = true
local Players = game:GetService("Players")
local function save(plr)
if doSaving then
local pets = {}
for parent, pet in pairs(plr.Pets:GetChildren()) do
table.insert(pets, pet.Name)
end
for parent, pet in pairs(plr.Character.EquippedPets:GetChildren()) do
table.insert(pets, pet.Name)
end
local success, errorMsg = pcall(function()
DS:SetAsync(plr.UserId .. "-key", pets)
end)
end
end
local function load(plr)
if doSaving then
local data = nil
local success, errorMsg = pcall(function()
data = DS:GetAsync(plr.UserId .. "-key")
end)
if data ~= nil then
for pets, pet in pairs(data) do
local foundpet = petsFolder:FindFirstChild(pet)
if foundpet then
foundpet:Clone().Parent = plr:WaitForChild("Pets")
end
end
end
end
return
end
Players.PlayerAdded:Connect(load)
Players.PlayerRemoving:Connect(save)
All help is appreciated!