It’s my first time using DataStoreService, so I just followed the tutorial, but it was only about saving single values. The problem is, there is a lot of “abilities” and it would be really bad looking to put all the values in one leaderstats folder (as I dunno how could I make it other way, I tried to use in pairs loop to create a bool value for every ability if the player has unlocked it or not). I did some “research”, and from what I found out you can’t really save folders with data store, so is there any other solution? If not, is there an easier and cleaner way to save huge amount of values?
local DataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataKey = "ParanormalsDataStoreKey"
local DataStore = DataStoreService:GetDataStore(dataKey)
local function Save(player)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
local playerKey = "Data/User_"..player.UserId
local Data = {}
for i,v in leaderstats:GetDescendants() do
if v:IsA("ValueBase") then
Data[v.Name] = v.Value
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(playerKey, Data)
end)
if not success then
warn("Data save failed for "..playerKey)
end
end
local function GetData(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("NumberValue")
kills.Name = "Kills"
kills.Parent = leaderstats
kills.Value = 0
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = leaderstats
cash.Value = 0
local abilities = Instance.new("Folder")
abilities.Parent = leaderstats
abilities.Name = "Abilities"
for i,v in game.ServerStorage.Abilities.Prices:GetDescendants() do
if v.ClassName == "NumberValue" or v.ClassName == "StringValue" then
local abval = Instance.new("BoolValue")
abval.Name = v.Name
abval.Parent = abilities
abval.Value = false
end
end
local playerKey = "Data/User_"..player.UserId
local success,r = pcall(function()
return DataStore:GetAsync(playerKey)
end)
if success then
if r then
for name,value in r do
local ins = leaderstats:FindFirstChild(name)
if not ins then continue end
ins.Value = value
end
end
else
warn("Data download failed for "..playerKey)
end
end
players.PlayerAdded:Connect(GetData)
players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for i,player in players:GetChildren() do
Save(player)
end
end)