-
What I want to achieve is to give this datastore an upgrade. And also is it possible for this datastore to handle 20+ Values for around 15 or 20 player in the same server without having any data failures/losses.
-
There is no issue yet, I just want to make sure this is good.
-
No solutions yet.
Down below is the code I currently use.
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("DataStore1")
players.PlayerAdded:Connect(function(plr)
local Stats = Instance.new("Folder")
Stats.Name = "Stats"
Stats.Parent = plr
local Points = Instance.new("IntValue")
Points.Parent = Stats
while task.wait(.5) do
Points.Value = math.random(1,1000) -- This is for a test
end
local data = saveDataStore:GetAsync(plr.UserId)
if data then
for name,value in pairs(data.Stats) do
local success, err = pcall(function()
Stats[name].Value = value
end)
end
end
end)
players.PlayerRemoving:Connect(function(plr)
local saveData = {Stats = {}}
for _,stat in pairs(plr.Stats:GetChildren()) do
local success, err = pcall(function()
saveData.Stats[stat.Name] = stat.Value
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
saveDataStore:SetAsync(plr.UserId,saveData)
end)
game:BindToClose(function()
for _,plr in pairs(players:GetPlayers()) do
local saveData = {Stats = {}}
for _,stat in pairs(plr.Stats:GetChildren()) do
local success, err = pcall(function()
saveData.Stats[stat.Name] = stat.Value
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
wait(7)
end
end)```