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")
local tries = 8 -- Change This How Many Want You Want
local dataLoaded = false
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(60) -- 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:GetAsync(key)
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(60) -- Slow Down The Process ; Repeats
end
until count >= tries or sucess
dataLoaded = 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
Players.PlayerRemoving:Connect(Set)
Players.PlayerAdded:Connect(createLeaderstats)
game:BindToClose(function()
for _, Players in ipairs(Players:GetPlayers()) do
coroutine.wrap(Set)
end
end)
NOTE : If your here to say use Data Store 2 then stop i want to learn on my own.
Question :
- Can i still throttled roblox on my repeat until sucess?
- Is my BindToClose is good using courtine wrap?
- Can UpdateAsync help me to not overite data?
Plans :
- I will change the GetAsync to UpdateAync so i can server lock it…
Question :
- do you think UpdateAsync can also use in Loading data ? and lock server just like ProfileService because the ProfileService use it when loading or saving data.