How would I make a pet datastore?

This is my datastore but what is the best way to implement pets?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSave")

local function SavePlayerData(player)

	local success,errormsg = pcall(function()

		local SaveData = {}

		for i,stats in pairs(player.leaderstats:GetChildren()) do

			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId,SaveData)
	end)

	if not success then
		return errormsg
	end			
end	

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character) --Check if player's Character is added
		player.Character.Humanoid.Died:Connect(function()
			local errormsg = SavePlayerData(player)

			if errormsg then	
				warn(errormsg)		
			end	
		end)
		local Stats = Instance.new("Folder")
		Stats.Name = "leaderstats"
		Stats.Parent = player

		local statstime = Instance.new("IntValue")
		statstime.Name = "Money"
		statstime.Parent = Stats

		local Data = SaveDataStore:GetAsync(player.UserId)
		if Data then



			for i,stats in pairs(Stats:GetChildren()) do

				stats.Value = Data[stats.Name]			
			end			
		else				
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)

	local errormsg = SavePlayerData(player)

	if errormsg then	
		warn(errormsg)		
	end	
end)

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

		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)

You could add SaveDataStore:SetAsync(“Pets”, pets) for pets and just send the pets value as a table to the SavePlayerData function. Store what pets the player owns by name in the pets table.

If you want a player’s pet purchases to save, maybe make a table with a list of all of the pets, and include how many they own, example:

local table = {
   ["cat"] = 0, --User owns none of the cat pet
   ["dog"] = 1 --User owns 1 dog
}

And then use httpservice to convert to json and save it
(

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.