Pet script not saving

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!

1 Like

dont put the pets inside the character, better put them in petfolder inside the player, and just clone a physical pet to the character with the given name from the petfolder

1 Like

Fixed the issue. Instead of moving the pet from the players pet folder I just duped the pet then moved the duped pet to the equipped pets folder and deleted this line of code:

for parent, pet in pairs(plr.Character.EquippedPets:GetChildren()) do
1 Like