Datastore Not Saving Using UpdateAsync

I am working on a points system and I followed a guide from another topic and still its not saving.

Here is the code for it.


local pointsData = game:GetService("DataStoreService"):GetDataStore("Points")
local backupData = game:GetService("DataStoreService"):GetDataStore("Backup")

game.Players.PlayerAdded:Connect(function(plr)
	
	local Stored = Instance.new("Folder", plr)
	Stored.Name = "leaderstats"
	
	local rank = Instance.new("StringValue", Stored)
	rank.Name = "Rank"
	rank.Value = plr:GetRoleInGroup(4596247)
	
	local points = Instance.new("NumberValue", Stored)
	points.Name = "Points"
	points.Value = pointsData:GetAsync(plr.UserId) or 0
	
	print(pointsData:GetAsync(plr.UserId))
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local leaderstats = plr:FindFirstChild('leaderstats')
	
	
	if leaderstats then
		local saveData = {
			leaderstats['Points'].Value
		}
	
		local Success, Error = pcall(function() -- Added pcall
			pointsData:UpdateAsync(plr.UserId, function(OldData) -- Calls the UpdateAsync() function
				return saveData
			end)
		end)
		
		if not Success then
			warn('Failed to save pointData for')
		end
	end
end)
game:BindToClose(function()
	if not game:GetService("RunService"):IsStudio() then
		return
	end
	
	local player = game:GetService("Players")
	local players = player:GetPlayers()
	
	for _, player in pairs(players) do
		local userId = player.UserId
		local points = pointsData:GetAsync(player.userId)
		
		if points then
			
			local success, result = pcall(function()
				
				backupData:SetAsync(player.userId, points)
			end)
			
			if not success then
				warn(result)
			end
		end
	end
	
end)

I would highly suggest using DataStore2, it’s not just more reliable, but for me even easier to use! Check this video by AlvinBlox: https://www.youtube.com/watch?v=hBfMfB0BwGA

1 Like

Ok I will try that out if it works I will reply

If you are testing in Studio this may be the culrprit, because the server will close way too quick compared to if you were to play on an Open Client

BindToClose makes sure that the datastore saves no matter where or what you test this on by stalling the server before shutdown. Also, studio access to api needs to be enabled of course.

Did you even read what I said? On studio, updating is highly RANDOM. You know it’s updating if the game is taking longer than usual to close. If it closes fast, then it’s not saving…

I have tested in studio and in game

Doesn’t matter. BindToClose nonetheless stalls the shutdown till the function has stopped yielding.

2 Likes

I have done this as the points are for promotions and I am trying to lose the least amount of data.

1 Like

Have you tried a simplified version and got that working? Just something simple such as saving total playtime where you just retrieve data and set data.

1 Like

Here is a copy of the original version

local pointsData = game:GetService("DataStoreService"):GetDataStore("Points")

game.Players.PlayerAdded:Connect(function(plr)
	
	local Stored = Instance.new("Folder", plr)
	Stored.Name = "leaderstats"
	
	local rank = Instance.new("StringValue", Stored)
	rank.Name = "Rank"
	rank.Value = plr:GetRoleInGroup(4596247)
	
	local points = Instance.new("NumberValue", Stored)
	points.Name = "Points"
	points.Value = pointsData:GetAsync(plr.userId)
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local PointsRightNow = plr.leaderstats.points.Value
	pointsData:SetAsync(plr.UserId, PointsRightNow)
end)

It did not work thats why I added BindToClose

What were the results with BindToClose?

1 Like

I got the same result I added it after reading this post DataStore not working - #7 by waterrunner

1 Like

This is a datastore handling script which I wrote a good while ago. It might be helpful to you if you take a look at it and compare my code to your code to see if you’re missing something.

For some reason I decided back then that it was a good idea to save the data every interval. Though you can easily change this to only happen on player removal and server close.

Edit: Here's an updated version which saves on BindToClose as well.
-- DATASTORE HANDLING
 
local _version = 0 -- Changing this will reset the datastore
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("TimePlayed" .. _version)
 
function getData(player)
	local success, data = pcall(function()
		return datastore:GetAsync(player.UserId)
	end)
	
	if success then
		return data
	else
		warn("error while trying to retrieve " .. player.Name .. "'s data")
	end
end
 
function saveData(player, _data)
	local success = pcall(function()
		return datastore:SetAsync(player.UserId, _data)
	end)
	
	if not success then
		warn("error while trying to save " .. player.Name .. "'s data")
	end
end
 
game.Players.PlayerAdded:Connect(function(player)
	local _data = getData(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local timeVal = Instance.new("NumberValue")
	timeVal.Name = "Time Played"
	timeVal.Value = _data or 0
	timeVal.Parent = leaderstats
end)

game:BindToClose(function()
	for i,  v in pairs(game.Players:GetChildren()) do
		local leaderstats = v:FindFirstChild("leaderstats")
		if leaderstats then
			saveData(v, leaderstats["Time Played"].Value)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local leaderstats = Player:FindFirstChild("leaderstats")
	if leaderstats then
		saveData(Player, leaderstats["Time Played"].Value)
	end
end)
 
spawn(function()
	while wait(1) do
		for i,  v in pairs(game.Players:GetChildren()) do
			local leaderstats = v:FindFirstChild("leaderstats")
			if leaderstats then
				leaderstats["Time Played"].Value = leaderstats["Time Played"].Value + 1
			end
		end
	end
end)
1 Like

Ok I am testing a change I made to see if that works