I've heard that UpdateAsync() is better SetAsync(). It's confusing for me, can someone explain it detail? thanks.
COMMUNITY QUESTION
How do you make a simple, secure DataStore?
1 answer
UpdateAsync() Takes whatever data whichever datastore already as, SetAsync() sets the data to something ignoring what data already existed. If either one fails, UpdateAsync() will simply leave the players data as it was before; SetAsync() will delete that players data. That is why UpdateAsync() is better.
The following codes will give you a Coins & Wins datastore system; place them in parts in Workspace/ServerScriptService
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("swginerreberbree7tdg")
game.Players.PlayerAdded:Connect(function(player)
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = player:WaitForChild("leaderstats")
local playerUserId = "Player_"..player.UserId
-- Load data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Wins.Value = data
end
Wins.Changed:Connect(function()
print("Wins changed, saving...")
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Wins.Value
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(playerUserId, function()
return data
end)
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Wins.Value
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(playerUserId, function()
return data
end)
end)
if success then
print("Data successfully saved!")
else
warn("There was an error!")
error(errormessage)
end
end)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("swgwgrreg4gtne7tdg")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Coins"
Points.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
-- Load data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Points.Value = data
end
Points.Changed:Connect(function()
print("Wins changed, saving...")
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(playerUserId, function()
return data
end)
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(playerUserId, function()
return data
end)
end)
if success then
print("Data successfully saved!")
else
warn("There was an error!")
error(errormessage)
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

9 comments
Yes, I understand UpdateAsync() now, but if you save wins every time it changes, there's a high chance of data loss because you're saving too much.
Yes, I understand UpdateAsync() now, but if you save wins every time it changes, there's a high chance of data loss because you're saving too much.
Yes, I understand UpdateAsync() now, but if you save wins every time it changes, there's a high chance of data loss because you're saving too much.
Yes, I understand UpdateAsync() now, but if you save wins every time it changes, there's a high chance of data loss because you're saving too much.
Yes, I understand UpdateAsync() now, but if you save wins every time it changes, there's a high chance of data loss because you're saving too much.
Yes, I understand UpdateAsync() now, but if you save wins every time it changes, there's a high chance of data loss because you're saving too much.
woah didn't mean to post so much.
Lol was wondering why i got so many notifications, the answer is you simply debounce the finishline/Coins
https://scriptinghelpers.org/questions/118719/how-to-add-a-debounce-to-prevent-people-gaining-many-wins-in-a-second