I’m not sure if this is the right way to use update async to save data, but knowing me it isn’t lol.
--// [Data Store] \\--
local DSS = game:GetService("DataStoreService")
local MoneyDataStore = DSS:GetDataStore("Money")
----------------------
game.Players.PlayerAdded:Connect(function(player)
local MoneyKey = player.UserId .. "-Money"
local MoneyData = MoneyDataStore:GetAsync(MoneyKey)
local Money, leaderstats = Instance.new("IntValue"), Instance.new("Folder")
Money.Name = "Money"
leaderstats.Name = "leaderstats"
Money.Parent = leaderstats
leaderstats.Parent = player
local success, errormessage = pcall(function()
end)
if success then
print("Success")
if MoneyData then
print("Data Found")
Money.Value = MoneyData
else
print("No data Found")
Money.Value = 5
end
elseif errormessage then
print(errormessage)
end
end)
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 20
end)
game.Players.PlayerRemoving:Connect(function(player)
local MoneyKey = (player.UserId .. "-Money")
local success, errormessage = pcall(function()
MoneyDataStore:UpdateAsync(MoneyKey, function(oldValue)
print("Update Async")
local MoneyValue = player.leaderstats.Money.Value
local newValue = oldValue or 0
newValue = MoneyValue
return newValue
end)
end)
if success then
print("Success")
elseif errormessage then
print(errormessage)
end
end)