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.