What do I want to achieve is:
I wanna use UpdateAsync if SetAsync fails
I tried making one but after doing tests it doesn’t seems to works (or it works I don’t really know)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderStats = Instance.new("Folder",Player)
leaderStats.Name = "Leaderstats"
local cash = Instance.new("NumberValue", leaderStats)
cash.Name = "Cash"
cash.Value = 0
local Oil = Instance.new("NumberValue", leaderStats)
Oil.Name = "Oil"
Oil.Value = 0
--Load data--
local data1 = myDataStore:GetAsync(Player.UserId.."-cash")
local data2 = myDataStore:GetAsync(Player.UserId.."-oil")
local Success, Errormessage = pcall(function()
data1 = myDataStore:GetAsync(Player.UserId.."-cash")
data2 = myDataStore:GetAsync(Player.UserId.."-oil")
end)
if Success then
cash.Value = data1
Oil.Value = data2
else
print("Error occured when trying to load data")
warn(Errormessage)
end
end)
--DataSaving--
game.Players.PlayerRemoving:Connect(function(player)
local success, errormesage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash", player.Leaderstats.Cash.Value)
myDataStore:SetAsync(player.UserId.."-oil", player.Leaderstats.Oil.Value)
end)
if success then
print("Data Saved Successfully")
else
local playerdata1 = {cash1 = player.Leaderstats.Cash.Value }
local playerdata2 = {oil1 = player.Leaderstats.Oil.Value }
if playerdata1 then
myDataStore:UpdateAsync(player.UserId,function(oldValue1)
print(playerdata1)
return playerdata1
end)
end
if playerdata2 then
myDataStore:UpdateAsync(player.UserId,function(oldValue2)
print(playerdata2)
return playerdata2
end)
end
print("An error occured when trying to save data. UpdateAsync has been used")
warn(errormesage)
end
end)
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("DateStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local leaderStats = Instance.new("Folder")
leaderStats.Name = "leaderstats"
leaderStats.Parent = Player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderStats
local oil = Instance.new("IntValue")
oil.Name = "Oil"
oil.Parent = leaderStats
local succ, res = pcall(function()
return DS:GetAsync("Stats_"..Player.UserId)
end)
if succ then
cash.Value = res[1]
oil.Value = res[2]
else
warn(res)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local succ, res = pcall(function()
DS:SetAsync("Stats_"..Player.UserId, {Player.leaderstats.Cash.Value, Player.leaderstats.Oil.Value})
end)
if succ then
return
else
warn(res)
end
end)
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("DateStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local leaderStats = Instance.new("Folder")
leaderStats.Name = "leaderstats"
leaderStats.Parent = Player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderStats
local oil = Instance.new("IntValue")
oil.Name = "Oil"
oil.Parent = leaderStats
local succ, res = pcall(function()
return DS:GetAsync("Stats_"..Player.UserId)
end)
if succ then
if res then
cash.Value = res[1]
oil.Value = res[2]
end
else
warn(res)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local succ, res = pcall(function()
DS:SetAsync("Stats_"..Player.UserId, {Player.leaderstats.Cash.Value, Player.leaderstats.Oil.Value})
end)
if succ then
return
else
warn(res)
end
end)