So i have here a datastore that saves a folder that includes values for a avatar editor/shop and when i leave the game it says it saves but when i get back in the game it will not have any values saved in the folder. Please help!
This is the code
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("CatalogItemDataStore")
game.Players.PlayerAdded:Connect(function(player)
local EventItemsInv = Instance.new("Folder")
EventItemsInv.Name = "CatalogItemsFolder"
EventItemsInv.Parent = player
local data
local success,errorMsg = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
if data ~= nil then
if data.Values then
for i, v in pairs(data.Values) do
local val = Instance.new("StringValue")
val.Name = v
val.Parent = EventItemsInv
end
end
end
end)
game:BindToClose(function()
-- will run when the server is about to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
local data = {}
data.Values = {}
for i, v in pairs(player.CatalogItemsFolder:GetChildren()) do
table.insert(data.Values, v.Name)
end
local success,errorMsg = pcall(function()
datastore:SetAsync(player.UserId,data)
end)
if success then
print("Success")
end
if errorMsg then
print("Error With the Catalog Save Has Occured.. (Roblox may be down)"..errorMsg)
end
end
end)
local trapDebounce = false
game.Players.PlayerRemoving:Connect(function(player)
local data = {}
data.Values = {}
for i, v in pairs(player.CatalogItemsFolder:GetChildren()) do
table.insert(data.Values, v.Name)
end
local success,errorMsg = pcall(function()
datastore:SetAsync(player.UserId,data)
end)
if success then
print("CatalogOwnedItems Saved = True")
end
if errorMsg then
print("Error found while trying to save data (are roblox datastore servers down?)"..errorMsg)
end
end)
2 Likes
Your code is super spaced out and I cant even see whats going on.
Maybe try the code I use:
local S_datastore = game:GetService("DataStoreService")
local D_Coins = S_datastore:GetDataStore("Coins")
local D_TotalCoins = S_datastore:GetDataStore("TotalCoins")
local D_Time = S_datastore:GetDataStore("Time")
local players = game.Players
local Serverstor = game.ServerStorage
local signals = Serverstor.SIGNALS
players.PlayerAdded:Connect(function(P)
wait(2)
local Player = game.Players:GetPlayerFromCharacter(P.Character)
local Pack = Serverstor.PlayerData:FindFirstChild(Player.Name)
if Player and Pack then
local A, Coins = pcall(D_Coins.GetAsync, D_Coins, Player.UserId)
local B, TotalCoins = pcall(D_TotalCoins.GetAsync, D_TotalCoins, Player.UserId)
local C, Time = pcall(D_Time.GetAsync, D_Time, Player.UserId)
if A == true and B == true and C == true then
Pack:FindFirstChild("Gained_Coins").Value = Coins
Pack:FindFirstChild("Total_Coins").Value = TotalCoins
Pack:FindFirstChild("Time").Value = Time
print("Succesfully loaded data of player!")
else
warn("Couldn't retrieve data from playerID"..Player.UserId)
end
else
warn("Player or Pack data sent doesnt exist and thus cant load.")
print(Player)
print(Pack)
end
end)
players.PlayerRemoving:Connect(function(Player)
--adding Pcall()
local Pack = Serverstor.PlayerData:FindFirstChild(Player.Name)
if Pack then
local tmp = Pack:FindFirstChild("Time")
local A, U = pcall(D_Time.SetAsync, D_Time, Player.UserId, tmp.Value)
tmp = Pack:FindFirstChild("Total_Coins")
local B, U = pcall(D_TotalCoins.SetAsync, D_TotalCoins, Player.UserId, tmp.Value)
tmp = Pack:FindFirstChild("Gained_Coins")
local C, U = pcall(D_Coins.SetAsync, D_Coins, Player.UserId, tmp.Value)
if A == true and B == true and C == true then
print("Succesfully Saved data!")
else
warn("Didn't save data completely of playerID "..Player.UserId)
end
Pack:Destroy()
else
warn("Couldnt find pack after player left, Data-PileUp imminent.")
end
end)
You can replace the packet system with something else if you really need.
DO NOT REMOVE THE 2 SECOND WAIT, Ive tried without, It breaks.
–goodluck!
1 Like
I need to have a datastore make a folder and save the values that are in the folder. The script is a lil complex
but thanks 
2 Likes
can someone pls help me with the issue
1 Like