I currently do my own data store by finding other source on YouTube.
However can somebody help me to improve this Data Store .
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
-- [[ SETTINGS]] --
local tries = 5 -- Retries
local Closing = 8 -- When shutdowns wait() dont make it higher than 8
local tries2 = 3 -- Dont set it higher than 4 because it will fail.
local Slowdown = 60 -- Slowdown
local dataLoaded = {} -- Tables per Saves.
local function Set(plr)
if dataLoaded then
local key = "plr-" .. plr.UserId
local count = 0
local data = {
["Coins"] = plr.leaderstats.Coins.Value,
}
local sucess, err
repeat
sucess, err = pcall(function()
myDataStore:UpdateAsync(key, function(oldValue)
local newValue = data or oldValue or 0
return newValue
end)
end)
count = count + 1
if script.Parent["Data - Store"].SelfCheck == true and count >= 2 then -- Prints The progress.
print(count.. " We retrying to load the data.")
end
if count >= 2 then
wait(Slowdown) -- Slow Down The Process ; Repeats
end
until count >= tries or sucess
if not sucess then
warn("Failed to set the value. Error code: " .. tostring(err))
return
end
else
return
end
end
local function Get(plr)
local key = "plr-" .. plr.UserId
local count = 0
local data
local sucess, err
repeat
sucess, err = pcall(function()
data = myDataStore:UpdateAsync(key, function(Value)
return Value
end)
end)
count = count + 1
if script.Parent["Data - Store"].SelfCheck == true and count >= 2 then -- Prints The progress.
print(count.. " We retrying to load the data.")
end
if count >= 2 then
wait(Slowdown) -- Slow Down The Process ; Repeats
end
until count >= tries or sucess
-- individual
dataLoaded[plr.UserId] = true
if not sucess then
warn("Failed to read. Data. Error code: " .. tostring(err))
plr:Kick("Sorry were not able to load your data kindly rejoin.")
return
end
if sucess then
if data then
return data
else
return {
["Coins"] = script.Parent["Data - Store"].Increment.Value,
}
end
end
end
local function createLeaderstats(plr)
local values = Get(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", leaderstats)
Coins.Name = "Coins"
Coins.Value = 0
Coins.Value = values.Coins
end
local function Closing(plr) -- When saving in shutdown...
if dataLoaded then
local key = "plr-" .. plr.UserId
local count = 0
local data = {
["Coins"] = plr.leaderstats.Coins.Value,
}
local sucess, err
repeat
sucess, err = pcall(function()
myDataStore:UpdateAsync(key, function(oldValue)
local newValue = data or oldValue or 0
return newValue
end)
end)
count = count + 1
if script.Parent["Data - Store"].SelfCheck == true and count >= 2 then
print(count.. " We retrying to load the data.")
end
if count >= 2 then
wait(Closing) -- Slow Down The Process ; Repeats
end
until count >= tries2 or sucess
if not sucess then
warn("Failed to set the value while BindToClose. Error code: " .. tostring(err))
return
end
else
return
end
end
Players.PlayerRemoving:Connect(Set)
Players.PlayerAdded:Connect(createLeaderstats)
local RunService = game:GetService("RunService")
game:BindToClose(function()
if (RunService:IsStudio())then
wait(1)
else
for i, v in next, game:GetPlayers() do
if v then
coroutine.wrap(Closing)(v)
end
end
end
end)
NOTE : If your here to say use Data Store 2 then stop i want to learn on my own.
Question :
- How can i stop to throttled roblox limits?
- For example i lost my data when loading can UpdateAsync help me to retrive it?
Any suggest for improvements?