Here is the code, it is not putting the (Pet).Name into the table PetsFolder, and saving and loading through DataStore? What am I doing wrong, or missing? Thanks in advance for anyone that can help fix the problem.
local players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local PetDataStore = DataStoreService:GetDataStore("TestingPets1") local dataLoaded = nil local dataAttempts = 3 local playerPets = {} local function get(plr) local pKey = "pPets_"..plr.UserId local serialized local count = 0 local success, err repeat success, err = pcall(function() serialized = PetDataStore:GetAsync(pKey) end) count = count + 1 wait(0.5) until count >= dataAttempts or success if not success then warn("Failed to read or load data. Error code: " .. tostring(err)) return end if serialized then return serialized else return {}; end end local function Set(plr) if dataLoaded then local pKey = "pPets_"..plr.UserId local data = playerPets local success, err local count = 0 for i, v in pairs(plr.Pets:GetChildren()) do table.insert(playerPets, v.Name) end repeat success, err = pcall(function() PetDataStore:SetAsync(pKey, data) end) count = count + 1 wait(0.5) until count >= dataAttempts or success if success then print(unpack(playerPets)) end if not success then warn("Failed to serialize data. Error code: " .. tostring(err)) return end end end local function CreatePets(plr) local PetsFolder = Instance.new("Folder") PetsFolder.Name = "Pets" PetsFolder.Parent = plr dataLoaded = true end players.PlayerAdded:Connect(CreatePets) players.PlayerRemoving:Connect(Set)
It is a Obvious Problem. You never told it to Load data from the DataStore.
get(plr)
Also you may also want to
game:BindToClose()
cause if you are the last player. The server can shutdown before the Data gets saved
local players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local PetDataStore = DataStoreService:GetDataStore("TestingPets1") local dataLoaded = nil local dataAttempts = 3 local playerPets = {} local function get(plr) local pKey = "pPets_"..plr.UserId local serialized local count = 0 local success, err repeat success, err = pcall(function() serialized = PetDataStore:GetAsync(pKey) end) count = count + 1 wait(0.5) until count >= dataAttempts or success if not success then warn("Failed to read or load data. Error code: " .. tostring(err)) return end if serialized then return serialized else return {}; end end local function Set(plr) if dataLoaded then local pKey = "pPets_"..plr.UserId local data = playerPets local success, err local count = 0 for i, v in pairs(plr.Pets:GetChildren()) do table.insert(playerPets, v.Name) end repeat success, err = pcall(function() PetDataStore:SetAsync(pKey, data) end) count = count + 1 wait(0.5) until count >= dataAttempts or success if success then print(unpack(playerPets)) end if not success then warn("Failed to serialize data. Error code: " .. tostring(err)) return end end end local function CreatePets(plr) local PetsFolder = Instance.new("Folder") PetsFolder.Name = "Pets" PetsFolder.Parent = plr local Data = get(plr) for _, Pet in pairs(Data) do -- Do some stuff right here end dataLoaded = true end players.PlayerAdded:Connect(CreatePets) players.PlayerRemoving:Connect(Set)