Saving a Table in Datastore

how can I save tables in datastore then get that table and use it in a for loop? ex:

--this is my current code that does not work
--its also edited for simplicity
local plrPets = {}

for i,pet in pairs(player.Pets:GetChildren()) do
   table.insert(plrPets, pet.Value) --the pets are stored in string values
end

datastore:SetAysnc("Pets_"..player.UserId, plrPets)

Getting the table:

local LoadedPets = {}
local success, err = pcall(function()
   LoadedPets = datastore:GetAysnc("Pets_"..player.UserId)
end)

if success then
   --add pets to folder
else
   warn("Unable to load pets!")
end

Also there are no errors. And yes I know im doing something wrong but I dont know what so I’m asking here!

1 Like

Have you tried a loop, like loop the function till an value or function is true or has acquired its reason like this is a simple example of it, and this just helps at debugging messy code or not very known code.

local Value = false
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
    Value = true
end)

while not Value do
   warn("Value is not true!") 
end

Please next time use the search button to search posts similar to this:

Is it possible to show us your full script?

You are putting the results of a GetChildren() call into a table and then trying to put that table into a datastore. You can’t save instances in a datastore. You will need to create a way of serializing the players’ pets, like converting them into a table of properties such as name, type, level, etc.

Save the name of your pets then when loading, search for those pets via name in a storage service to give back the pets.

yes I am aware. they are stored in string values and “pet” in the for loop was suppose to be pet.value which was my mistake