How to save the player inventory pets?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to save all pets in the player’s inventory, but all these pets are a TextButton, I searched and found that DataStore only saves strings. The pet in the print below will be an exclusive pet for administrators, so it will not need to be saved, as it is added to the administrator’s inventory every time he enters the game. The video shows the pets that players will be able to get, and those will be saved.

  2. What is the issue? Include screenshots / videos if possible!
    The DataStoreService just save strings!
    image
    robloxapp-20230819-1855202.wmv (1.9 MB)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I searched the Roblox Studio forum, but I found few similar things, and the ones I found I tried to adapt and see if it worked, but it didn’t work.

I tried creating a folder inside the player called Pets, where every time a new Pet was added to the player’s inventory, it would be replicated in that folder as a string value, but I found something impossible to solve. The pet was added to the Pets folder, but even after saving, when re-entering the game, the pets did not appear in the Pets folder, nor were they replicated in the inventory.

1 Like
local DataStore=game:GetService('DataStoreService')
local PetsData=DataStore:GetDataStore('PetsData')
local plrData={
	['Pets']={};
}

table.insert(plrData['Pets'],'PETNAME')
--- // If you want players to have individual data insert plr.UserId into the plrData table and then add the ['Pets'] in that.

Just a quick like 5 second thing I wrote up, this is how you should go about an inventory System. Use GetAsync and SetAsync to grab and save data.

Datastores are capable of saving tables fyi

First you’re gonna wanna create a datastore

function LoadDSS(userid)
     local DSS = game:GetService("DataStoreService")
     local PlayerDataStore = DataStore:GetDataStore(userid) 

     if not (PlayerDataStore) then 
          DataStore:GetDataStore(userid):SetAsync("PLR_DATA", { 
               ["Pets"] = {<TABLE OF DEFAULT PETS>}, --Set default pets
          )}
         return DataStore:GetDataStore(userid):GetAsync("PLR_DATA")
     else 
          return DataStore:GetDataStore(userid)):GetAsync("PLR_DATA")
     end
end

function WriteDSS(SetAs) --You will need to set this function to everything you want saved, not just everything you want added.
     local DataStore= game:GetService("DataStoreService")
     local PlayerDataStore = DataStore:GetDataStore(userid)

      DataStore:GetDataStore(userid):SetAsync("PLR_DATA", SetAs)
end

You can add more items after the “Pets” table because of the way I set it up to be saved as a table inside a table.

My first code was incorrect, I have corrected it.

1 Like

i found this script and tried to adapt it but still it is not saving

-- Services
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Instances
local DataStore = DataStoreService:GetDataStore("DataStore#01")

-- Loading data
local function Load(player)
	local PlayerData

	-- Gets the player's data inside a secure function
	local Success, Error = pcall(function()
		PlayerData = DataStore:GetAsync(player.UserId)
	end)

	-- Kicks the player if there was an error loading the data
	if not Success then
		player:Kick("Error loading data: " .. Error)
	end

	-- Creates a new folder for the player's pets
	local PetsFolder = Instance.new("Folder")
	PetsFolder.Name = "Pets"
	PetsFolder.Parent = player
	
	player.PlayerGui:WaitForChild("Inventory").ScrollingFrame.ChildAdded:Connect(function(c)
		local Pet = Instance.new("StringValue")
		Pet.Name = 'Pet'
		Pet.Value = c.Name
		Pet.Parent = PetsFolder
	end)
	
	-- If the player has got data (not first time playing) then
	-- loop through all of their pets, creating a new value for it
	if PlayerData then
		for _, PetData in pairs(PlayerData) do
			local Pet = Instance.new("StringValue")
			Pet.Name = 'Pet'
			Pet.Value = PetData.Name
			Pet.Parent = PetsFolder
		end
	end
end

-- Saving Data
local function Save(player)
	local PetsFolder = player.Pets
	if not PetsFolder then
		return -- The player hasn't created a pets folder, nothing will be saved
	end

	local PlayerData = {}

	-- Looping through all of the player's pets
	for _, Pet in pairs(PetsFolder:GetChildren()) do
		if Pet:IsA("TextButton") then
			-- Add the pet's name and value to the data, then add it to the main table
			local Data = {
				Name = Pet.Name
			}
			table.insert(PlayerData, Data)
		end
	end

	-- Save the main table in a secure function
	pcall(function()
		DataStore:SetAsync(player.UserId, PlayerData)
	end)
end

-- Detect player's joining & leaving
Players.PlayerAdded:Connect(Load)
for _, Player in pairs(Players:GetPlayers()) do
	Load(Player)
end
Players.PlayerRemoving:Connect(Save)
2 Likes

basically it creates a folder within the player’s object upon entry, and every new pet added to the player’s inventory is added to the pets folder. line 1 of the Save function was supposed to be “local PetsFolder = player.PlayerGui.Inventory.ScrollingFrame”, where it would take the pets in the player’s inventory gui and save them in the Pets folder in string format, but it didn’t work

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