Saving pets and pet data?

I’ve been trying to figure this out for weeks. I can’t figure out how to make a datastore to save the pets the player has and those pet’s stats. I would assume since its multiple data per slot it’d have to be a table with the slot’s name, then all of the stats, but I can’t figure it out.

this is the hierarchy, I just need to figure out how to save the slots name and its data inside of it.
(the data is values)
[DataTransfer is something else, disregard it.]
image

You can just savr the pets name there a tutoral that has this yo work but you saving all of the part data do just change it to the name and iust clone it from what folder has the pet in it and if the pets are in diffent folders you can just do a for loop yhourgh the game ill make a virson soon

Edit: and the attbrute/numbervales too

Can you send the tutorial? I haven’t seen one.

sorry i was so late i was doing something the link is https://www.youtube.com/watch?v=j0b-ULMGL7Y&t=0s

this is if the pets are all be stored in one single folder ( i image there not) so i will also make one with if there isn’t

local DataStoreService = game:GetService("DataStoreService")
local PetData = DataStoreService:GetDataStore("PetsData")
local Players = game.Players

local KeyPrefix = 'Player: '
local Pets = game:GetService("ReplicatedStorage").Pets -- Change To The Correct Loacton

function PlayerAdded(Player: Player)
	local PetInvenotry = Instance.new("Folder")
	PetInvenotry.Name = "PetInvenotry" -- Change To The Name
	PetInvenotry.Parent = Player

	
	local Key = KeyPrefix..tostring(Player.UserId)
	
	local Success = nil
	local DataStore = nil

	local Attempt = 1
	repeat
		Success, DataStore = pcall(function()
			return  PetData:GetAsync(Key)
end)
if not Success  then
	print(DataStore)
	Attempt += 1
	task.wait(2)
end
	until Success or Attempt == 5 or not game.Players[Player.Name]
	if not DataStore then return end
	
	if Success then
		for _, oby in ipairs(DataStore) do
			local Pet = Pets:FindFirstChild(oby[1])
			if Pet then
				local NewPet = Pet:Clone()
				NewPet.Parent = PetInvenotry
				NewPet:SetAttribute("damge", oby[2])
				NewPet:SetAttribute("Raity", oby[3])
				print(NewPet:GetAttribute("Raity"))
				print(NewPet.Name)
			end
		end
	else
		print("Failed To Load Data "..DataStore)
	end
	
end

function PlayerLefted(Player: Player)
	local Key = KeyPrefix..tostring(Player.UserId)
	local data = {}
	local PetInvenotry = Player.PetInvenotry
	
	Pets:GetChildren()
	
	for _, Pet: Model? in ipairs(PetInvenotry:GetChildren()) do
		table.insert(data,{
			Pet.Name,
			Pet:GetAttribute("damge"), -- change this to the attribute or number values name
			Pet:GetAttribute("Raity") -- chnage to name
		})
		
		local Success = nil
		local Fail = nil
		
		local Attempt = 1
		repeat
			Success, Fail = pcall(function()
				PetData:SetAsync(Key,data)
			end)
			if not Success  then
				print(Fail)
				Attempt += 1
				task.wait(2)
			end
			
		until Success or Attempt == 5
		if Success then
			print("Success")
		else
			print("Creeper AW MAN")
		end
		end
	end


Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLefted)

game:BindToClose(function()
	for i, plr in ipairs(Players:GetPlayers()) do
		PlayerLefted(plr)
	end
		
end)


The if they are in a egg script i imge they are

local DataStoreService = game:GetService("DataStoreService")
local PetData = DataStoreService:GetDataStore("PetsData")
local Players = game.Players

local KeyPrefix = 'Player: '
local CollenctionSerivce = game:GetService("CollectionService")
local Tag = "_Pets" -- chnge to your tag name or make one

function PlayerAdded(Player: Player)
	local PetInvenotry = Instance.new("Folder")
	PetInvenotry.Name = "PetInvenotry" -- Change To The Name
	PetInvenotry.Parent = Player

	
	local Key = KeyPrefix..tostring(Player.UserId)
	
	local Success = nil
	local DataStore = nil

	local Attempt = 1
	repeat
		Success, DataStore = pcall(function()
			return  PetData:GetAsync(Key)
end)
if not Success  then
	print(DataStore)
	Attempt += 1
	task.wait(2)
end
	until Success or Attempt == 5 or not game.Players[Player.Name]
	if not DataStore then return end
	
	if Success then
		for _, oby in ipairs(DataStore) do
			for _, Egg in ipairs(CollenctionSerivce:GetTagged(Tag)) do
					local Pet = Egg:FindFirstChild(oby[1])
			if not  Pet then continue end
				local NewPet = Pet:Clone()
				NewPet.Parent = PetInvenotry
				NewPet:SetAttribute("damge", oby[2])
				NewPet:SetAttribute("Raity", oby[3])
				print(NewPet:GetAttribute("Raity"))
				print(NewPet.Name)
			end
		end
	else
		print("Failed To Load Data "..DataStore)
	end
	
end

function PlayerLefted(Player: Player)
	local Key = KeyPrefix..tostring(Player.UserId)
	local data = {}
	local PetInvenotry = Player.PetInvenotry
	
	
	for _, Pet: Model? in ipairs(PetInvenotry:GetChildren()) do
		table.insert(data,{
			Pet.Name,
			Pet:GetAttribute("damge"), -- change this to the attribute or number values name
			Pet:GetAttribute("Raity") -- chnage to name
		})
		
		local Success = nil
		local Fail = nil
		
		local Attempt = 1
		repeat
			Success, Fail = pcall(function()
				PetData:SetAsync(Key,data)
			end)
			if not Success  then
				print(Fail)
				Attempt += 1
				task.wait(2)
			end
			
		until Success or Attempt == 5
		if Success then
			print("Success")
		else
			print("Creeper AW MAN")
		end
		end
	end


Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLefted)

game:BindToClose(function()
	for i, plr in ipairs(Players:GetPlayers()) do
		PlayerLefted(plr)
	end
		
end)


I have this script right now, what do I need to do to make it work?

local players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local datastore = DSS:GetDataStore("EggData")

local function save(player: Player)
	local playerGui = player:FindFirstChild("PlayerGui")
	if not playerGui then return end

	local inventoryGui = playerGui:FindFirstChild("Inventory")
	if not inventoryGui then return end

	local container = inventoryGui.BG.Container

	local key = player.UserId
	local data = {}

	for _, value in ipairs(container:GetChildren()) do
		if value:IsA("TextButton") then
			table.insert(data, {
				value.Name,
				value.CurrentSteps.Value,
				value.DateObtained.Value,
				value.EggName.Value,
				value.RequiredSteps.Value
			})
		end
	end

	local success, err = pcall(function()
		datastore:SetAsync(key, data)
	end)

	if not success then
		warn("Failed to save data for player: " .. player.Name .. ". Error: " .. tostring(err))
	end
end

local function load(player: Player)
	local playerGui = player:WaitForChild("PlayerGui")
	local inventoryGui = playerGui:WaitForChild("Inventory")
	local container = inventoryGui.BG.Container
	local templateSlot = container.TemplateSlot

	local key = player.UserId
	local data
	local success, err = pcall(function()
		data = datastore:GetAsync(key)
	end)

	if success and data then
		for _, value in ipairs(data) do
			local slotClone = templateSlot:Clone()
			slotClone.Name = value[1]
			slotClone.CurrentSteps.Value = value[2]
			slotClone.DateObtained.Value = value[3]
			slotClone.EggName.Value = value[4]
			slotClone.RequiredSteps.Value = value[5]
			slotClone.Visible = true
			slotClone.Parent = container
		end
	elseif not success then
		warn("Failed to load data for player: " .. player.Name .. ". Error: " .. tostring(err))
	end
end

players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for _, player in ipairs(players:GetPlayers()) do
		save(player)
	end
end)

Sorry about not replying yet but what functions not wotking the save or load or are they boyh not working?

Also add repeat untill loops on setting async and getting async so it insures it gers the data more often

Its just the fact im pretty sure I can’t get the playergui from a serverscript, because it just saves a blank table.

yes that is the problem because wince you are making the changes on the client side it wont change on the sever so you will be to fire a remote event to the client then the client fires back with the info

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