Hello,
I’m using this new method of storing data that I’ve never used before. This is my first attempt at using this method (but I’m not new to datastores themselves though).
Is there anything I should change? Is it efficient? Will it work in mass?
-- services
local DSS = game:GetService("DataStoreService")
local players = game:GetService("Players")
local RS = game:GetService("RunService")
--datastore locals
local datastore = DSS:GetDataStore("datastore")
-- getting the player
players.PlayerAdded:Connect(function(player)
--folders
local achievements = Instance.new("Folder") -- achievements
achievements.Name = "achievements"
achievements.Parent = player
local inventory = Instance.new("Folder") -- achievements and actives / passives
inventory.Name = "inventory"
inventory.Parent = player
local hiddenstats = Instance.new("Folder") -- everything else
hiddenstats.Name = "hiddenstats"
hiddenstats.Parent = player
-- values
local scrap = Instance.new("IntValue") -- scrap currency
scrap.Name = "scrap"
scrap.Parent = hiddenstats
local wins = Instance.new("IntValue") -- wins obtained
wins.Name = "wins"
wins.Parent = hiddenstats
local playtime = Instance.new("NumberValue") -- total time played
playtime.Name = "playtime"
playtime.Parent = hiddenstats
local dailystreak = Instance.new("IntValue") -- daily reward streak
dailystreak.Name = "dailystreak"
dailystreak.Parent = hiddenstats
local achievements = {} -- achievements unlocked
local passives = {} -- passive abilities
local actives = {} -- active abilties
-- edit to get the standard values
local nildata = {
[scrap ] = 0,
[wins] = 0,
[playtime] = 0,
[dailystreak] = 0,
[achievements] = {},
[passives] = {},
[actives] = {}
}
-- add to this with instance names
local datanames = {
[scrap] = 1,
[wins] = 2,
[playtime] = 3,
[dailystreak] = 4,
[achievements] = 5,
[passives] = 6,
[actives] = 7
}
local data
local success, errormessage = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
-- loading data
local function updata()
if data == nil then
for key, value in nildata do -- if the player is new and has no data
key.Value = value -- iterate through the table and set the values to the dictionary values
end
else
for key, value in nildata do -- if the player has previous data
key.Value = data[datanames[key]] -- load it and use datanames as an inverse table to get the index
end
end
end
if success then
print(player,'\'s data has been successfully loaded')
updata()
else
print(player, '\'s data has an error while loading')
warn(errormessage)
updata()
end
-- save data
local function savedata()
local save = {} -- new empty table
for key, value in nildata do
table.insert(save, key.Value) -- save the instance's Value to the table
end
local success, errormessage = pcall(function() -- standard pcalling
datastore:SetAsync(player.UserId, save)
end)
if success then
print(player,'\'s data has been successfully saved')
else
print(player,'\'s data has an error while saving')
warn(errormessage)
end
end
-- save every 10 mins
game:GetService("Players").PlayerAdded:Connect(function(player)
while task.wait(600) do
savedata()
end
end)
-- save on leave
game:GetService("Players").PlayerRemoving:Connect(function(player)
savedata()
end)
if not RS:IsStudio() then
game:BindToClose(function()
if #players:GetPlayers() <= 1 then return end
savedata()
end)
end
end)
Thank you!