I currently do my DataStore and i have alot of questions when i’m doing this kind of data store.
| Data Store |
local Players = game:GetService("Players ")
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)
-- [[ SETTINGS]] --
local tries = 3 -- Retries
local Closing = 8 -- When shutdowns wait() dont make it higher than 8
local tries2 = 3 -- Dont set it higher than 4 because its part of BindToClose()
local Slowdown = 65 -- Slowdown Get / Update
local IsInGame = {} -- In this variable will verify if Player still in game.
local dataLoaded = {} -- Tables per Saves.
local function Set(plr)
local key = "plr-" .. plr.UserId
IsInGame[key] = true -- Means Closed Case
if dataLoaded[key] == true then -- Make sure it loads
local count = 0
local data = {
["Coins"] = plr.leaderstats.Coins.Value,
}
local sucess, err
--[[
<Descriptions>
- As we all know roblox cant save data within 60 seconds on PlayerRemoving so sometimes the repeat;
until sucess here wont not function.
]]
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, value)
local key = "plr-" .. plr.UserId
IsInGame[key] = false -- When they just joim
local count = 0
local data
local sucess, err
repeat
if IsInGame[key] == false then
sucess, err = pcall(function()
data = myDataStore:GetAsync(key)
end)
count = count + 1
if script.SelfCheck.Value == 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
end
until count >= tries or sucess
-- individual
dataLoaded[key] = true
if not sucess then
warn("Failed to read. Data. Error code: " .. tostring(err))
plr:Kick("Sorry were not able to load your data roblox is currently down please try again in next time.")
return
end
if sucess then
if data then
return data
else
return {
["Coins"] = script.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...
local key = "plr-" .. plr.UserId
IsInGame[key] = true -- Means Closed Case
if dataLoaded[key] == true then
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.SelfCheck.Value == 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)
Questions :
- In my Slowdown Can i prevent throttling Roblox since its more than 60 seconds
- How to stop this code to Lock the server? I know UpdateAsync can lock server but how to stop locking the server after the data Loaded.
How can i improve this DataStore :
- Should i add AutoSaving in this code
NOTE :
- When youre here to say use DataStore2 or ProfileService then sorry i’m learning on my own.
Thank you.