DataStore Not saving when Using BlindToClose

I am making a datastore leaderstats with a datastore.
Not Using DataStore2 or Profile Service by the way.

local DataStoreService = game:GetService('DataStoreService')
local PlayerPoints = DataStoreService:GetOrderedDataStore("PlayerPoints")

game.Players.PlayerAdded:Connect(function(Player)
	local stats = Instance.new('Folder', Player)
	stats.Name = "leaderstats"
	local points = Instance.new('NumberValue', stats)
	points.Name = "Points"
	local isGet,Error = pcall(function()
		return PlayerPoints:GetAsync(Player.UserId) or 0
	end)
	if isGet and not Error then
		points.Value = isGet
	end
	if Error then
		if Error == 0 then return end
		error(Error)
	end
	points.Changed:Connect(function()
		local sus,err = pcall(function()
			PlayerPoints:SetAsync(points.Value, true)
		end)
		if sus then
			print('Saved!')
		end
		if err then
			error(err)
		end
	end)
	game:BindToClose(function() -- BlindToClose if the game shuts down.
		local sus,err = pcall(function()
			PlayerPoints:SetAsync(points.Value, true)
		end)
		if sus then
			print('Saved!')
		end
		if err then
			error(err)
		end
	end)
	game.Players.PlayerRemoving:Connect(function()
		local sus,err = pcall(function()
			PlayerPoints:SetAsync(points.Value, true)
		end)
		if sus then
			print('Saved!')
		end
		if err then
			error(err)
		end
	end)
end)

I don’t want any advertisements or anything.

This code is really unorganized. I just don’t understand why you have 3 events inside a PlayerAdded event. (I’m on mobile so this might be really bad). Try something along the lines of

game.Players.PlayerAdded:Connect(function(Player)

—Add the leaderstats here

end)



game:BindToClose(function()

—save all of the player’s stats

end)

game.Players.PlayerRemoving:Connect(function(Player)

—save the leaving player’s stats.

end)

Since game:BindToClose() executes when the server shuts down, we must save ALL of the players’ data.

For your SetAsync, the arguments are key and value. The key is the string type, the value is Variant (can be bool, number, etc.)

What I think you are trying to do is this: PlayerPoints:SetAsync(tostring(Player.UserId), points.Value)

Keep your PlayerRemoving and BindToClose out of the PlayerAdded function, they aren’t meant in there.

And for the BindToClose, you should use a for loop on each player to save each player’s data before BindToClose. Remember, BindToClose runs before the game shutdown.