Hi this is my first post on the forum. I think this is the right category.
I wanted to update player’s data using UpdateAsync(). I don’t understand much about it but after a few attempts, I got it working some how. Is this the right way?
local dataStore = game:GetService("DataStoreService")
local expStore = dataStore:GetDataStore("Exp")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local exp = Instance.new("IntValue")
exp.Name = "Exp"
exp.Parent = leaderstats
local data
local success, errorMessage = pcall(function()
data = expStore:GetAsync(player.UserId.."-exp")
end)
if success then
exp.Value = data
else
print("Error")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
expStore:UpdateAsync(player.UserId.."-exp",function(oldData)
local updatedData = oldData or 0
updatedData = player.leaderstats.Exp.Value
return updatedData
end)
end)
if success then
print("Success")
else
print("Error")
warn(errorMessage)
end
end)
Thanks!