Why is my pet saving system not working

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local DataStore = DataStoreService:GetDataStore("PetsStore")

Players.PlayerAdded:Connect(function(player)
	local ownedPets = Instance.new("Folder", player)
	ownedPets.Name = "OwnedPets"
	
	local petsData = DataStore:GetAsync(player.UserId.." Pets")
	
	if petsData ~= nil then
		for i, pet in pairs(petsData) do
			local petValue = Instance.new("StringValue", player:WaitForChild("OwnedPets"))
			petValue.Name = HttpService:GenerateGUID(false)
			petValue.Value = pet
			
			replicatedStorage.Remotes.CreatePet:FireClient(player, petValue.Name)
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local pets = {}
	
	for i, pet in pairs(player.OwnedPets:GetChildren()) do
		table.insert(pets, pet.Value)
	end
	
	DataStore:SetAsync(player.UserId.." Pets", pets)
end)
replicatedStorage.Remotes.CreatePet.OnClientEvent:Connect(function(petName)
	_G.createNewTemplate(petName)
end)

this may be because you forgot to add an extra scenario (the game binding to close)

you can see details about this here:

1 Like