How do i convert this to update async that save new value
local DataStoreService = game:GetService("DataStoreService") -- DataStore Variable
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", leaderstats)
Coins.Name = "Coins"
Coins.Value = 0
local playerUserId = "Player_"..player.UserId
--Load Data
local data
local sucess, errormessage = pcall(function()
wait(0.05)
data = myDataStore:GetAsync(playerUserId)
end)
Coins.Value = data
if sucess then
data = myDataStore:GetAsync(playerUserId)
elseif not sucess then
player:Kick("Your coins data dont load properly try to rejoin.")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
wait()
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Coins.Value
local sucess, errormessage = pcall(function()
wait()
myDataStore:SetAsync(playerUserId, data)
end)
if sucess then
wait()
else
wait()
warn(playerUserId.. " There was an error while saving the data of coins.")
wait()
myDataStore:SetAync(playerUserId, data)
end
end)