Save multiple names in a datastore?

I’m trying to make a datastore but fore some reason I can’t figure out how to save all the pet names!

local dts = game:GetService("DataStoreService")
local petsData = dts:GetDataStore("Pets")
local pets = game.ReplicatedStorage.pets

local function SavePlayerData(Player)
	local Pets = Player:WaitForChild("Pets")
	if Pets then
		for _, child in pairs(Pets:GetDescendants()) do
			petsData:SetAsync(child.Name, Player.UserId)
		end
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	local clone = script.Pets:Clone()
	clone.Parent = Player
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local errormsg = SavePlayerData(player)
	if errormsg then
		warn(errormsg)
	end			
	wait(2)
end)

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

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

It looks like you’re trying to save the names of the player’s pets to a data store, but you’re currently only saving the name of each pet to the data store as the key, and the player’s user ID as the value.

To save all of the pet names to the data store, you’ll need to store them in a data structure that can hold multiple values, such as a table. You can then save this table to the data store using the SetAsync function.

Here’s an example of how you can modify your code to save all of the pet names to the data store:

local function SavePlayerData(Player)
	local Pets = Player:WaitForChild("Pets")
	if Pets then
		local petNames = {}
		for _, child in pairs(Pets:GetDescendants()) do
			table.insert(petNames, child.Name)
		end
		petsData:SetAsync(Player.UserId, petNames)
	end
end

This will create a table called petNames that contains all of the pet names, and then save this table to the data store using the player’s user ID as the key. You can then retrieve the table of pet names for a specific player by using their user ID as the key when calling the GetAsync function on the data store.

fun fact, everything up to this point was written by an AI! everything else was written by me

I also noticed you’re calling the SavePlayerData() function in the PlayerRemoving() and BindToClose() functions and expecting the errormessage value in return, but you’re not returning anything in the SavePlayerData() function. To fix this, you have to use pcall and return the errormessage.

local function SavePlayerData(Player)
	local Pets = Player:WaitForChild("Pets")
	if Pets then
		local success, errormsg = pcall(function()
			local petNames = {}
			for _, child in pairs(Pets:GetDescendants()) do
				table.insert(petNames, child.Name)
			end
			petsData:SetAsync(Player.UserId, petNames)
		end)
		if not success then
			return errormsg
		end
	end
end

I hope this helps! Let me know if you have any questions or if you need further clarification.

You don’t need to answer this but is there a way to print all the names in the table on by one when the player joins?

Yes, there is. Could you also show your join function after? (the one where you load the player’s data)

When loading the player’s data using GetAsync(), you will receive a table with the pet names.

-- example
data = {
[1] = "Bob",
[2] = "Stacy",
[3] = "Joe"
}

You can print each name using a for loop, like so:

for _, v in pairs(data) do
   print(v)
end

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