Hello,
I am trying to save tables for the first time and I don’t get it seem to work, with other words. My data doesn’t save, and how does that come?
Leaderstats don’t need a save, btw.
[https://gyazo.com/20a023394c81192ff9b587f46b0b3a5c]
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local plrData = DataStoreService:GetDataStore("PlrData")
local LSFolder = script.leaderstats
local StatFolder = script.Stats
Players.PlayerAdded:Connect(function(plr)
local GetData = nil
local UserKey = "Player_" .. plr.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Stats = Instance.new("Folder")
Stats.Name = "Stats"
Stats.Parent = plr
for _, v in pairs(LSFolder:GetChildren()) do
local lsStat = Instance.new(v.ClassName)
lsStat.Name = v.Name
lsStat.Value = v.Value
lsStat.Parent = leaderstats
end
for _, v in pairs(StatFolder:GetChildren()) do
local Stat = Instance.new(v.ClassName)
Stat.Name = v.Name
Stat.Value = v.Value
Stat.Parent = Stats
end
local success, err = pcall(function()
GetData = plrData:GetAsync("Player_"..UserKey)
end)
if success then
print("Getting data from "..plr.Name)
if GetData then
for i, v in pairs(GetData) do
leaderstats[i].Value = v
end
print("Data Loaded For: "..plr.Name)
else
print("Data Created For: "..plr.Name)
end
else
warn("Error Getting Data For: "..plr.Name)
end
end)
Players.PlayerRemoving:Connect(function(plr)
local UserKey = plr.UserId
local StatsFolder = plr.Stats
local StatTable = {}
for i, v in pairs(StatsFolder:GetChildren()) do
table.insert(StatTable, v.Value)
end
local Success, Err = pcall(function()
plrData:UpdateAsync(UserKey, function(oldData)
local SaveTable = oldData or StatTable
for _, v in pairs(StatFolder:GetChildren()) do
SaveTable[v.Name] = v.Value
end
return SaveTable
end)
end)
if Success then
print("Data Has Been Saved For: "..plr.Name)
else
warn("Data Hasn't Been Saved For: "..plr.Name)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
for i, plr in pairs(Players) do
local UserKey = plr.UserId
local StatTable = {}
local StatsFolder = plr.Stats
for i, v in pairs(StatsFolder:GetChildren()) do
table.insert(StatTable, v.Value)
end
local Success, Err = pcall(function()
plrData:UpdateAsync(UserKey, function(oldData)
local SaveTable = oldData or StatTable
for _, v in pairs(StatFolder:GetChildren()) do
SaveTable[v.Name] = v.Value
end
return SaveTable
end)
end)
end
end)