Not really familiar with Datastores, will this work to save and load multiple stats at once?
and if not what is another way I can use to do that?
local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("GameData")
players.PlayerAdded:Connect(function(player)
local Int = Instance.new("IntValue",player) Int.Name = ("leaderstats")
local kos = Instance.new("NumberValue",Int) kos.Name = ("KOs")
local points = Instance.new("NumberValue",Int)
points.Name = "Points"
points.Value = 15000
local vc = Instance.new("NumberValue",Int) vc.Name = ("VirtualCurrency")
local lvl = Instance.new("NumberValue",Int) lvl.Name = ("lvl")
local xp = Instance.new("NumberValue",lvl) xp.Name = ("XP")
local Swords = Instance.new("Folder",player) Swords.Name = "Swords"
wait(1)
for a, b in ipairs(player.leaderstats:GetDescendants()) do
local stuff = GameData:GetAsync(player.UserId.."-"..b.Name)
b.Value = stuff
print("loading "..b.Name)
end
end)
players.PlayerLeaving:Connect(function(player)
for a, b in ipairs(player.leaderstats:GetDescendants()) do
GameData:SetAsync(player.UserID.."-"..b.Name, b.Value)
print("Saving "..b.Name.." "..b.Value)
end
end)
local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("GameData")
players.PlayerAdded:Connect(function(player)
local Int = Instance.new("IntValue",player) Int.Name = ("leaderstats")
local kos = Instance.new("NumberValue",Int) kos.Name = ("KOs")
local points = Instance.new("NumberValue",Int)
points.Name = "Points"
points.Value = 15000
local vc = Instance.new("NumberValue",Int) vc.Name = ("VirtualCurrency")
local lvl = Instance.new("NumberValue",Int) lvl.Name = ("lvl")
local xp = Instance.new("NumberValue",lvl) xp.Name = ("XP")
local Swords = Instance.new("Folder",player) Swords.Name = "Swords"
wait(1)
local success, err = pcall(function()
local stuff = GameData:GetAsync(player.UserId.."-StatsData")
for a, b in ipairs(stuff) do
for c, d in ipairs(player.leaderstats:GetDescendants()) do
if d.Name == a then
d.Value = b
end
end
end
end)
if success then
print("loaded data for "..player.Name)
else
print("loading data for "..player.Name.." failed.",err)
end
end)
local function Save(player)
local DataTable = {}
for a, b in ipairs(player.leaderstats:GetDescendants()) do
DataTable[b.Name] = b.Value
end
local success, err = pcall(function()
GameData:SetAsync(player.UserID.."-StatsData", DataTable)
end)
if success then
print("saved data for "..player.Name)
else
print("saving data for "..player.Name.." failed.",err)
end
end
players.PlayerLeaving:Connect(function(player)
Save(player)
end)
game:BindToClose(function()
for i,player in pairs(game.Players:GetChildren()) do
pcall(Save,player)
end
end)