How do I save ALL leaderstat values in a separate script?

The problem is that I previously made a script that does this, but it uses DataPersistence, which is deprecated.

I tried to find a script that does this but with DataStores instead of DataPersistence. I don’t know how to save all values with a DataStore, and all other scripts I found couldn’t either.

Just a reminder, I want the script to save every value in the leaderstats folder regardless of name. I also want the script to not be joined with the leaderstats script.

The script below only saves one value.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
game.Players.PlayerAdded:connect(function(player)
	local Cash = player.leaderstats.BloodBucks
	ds:SetAsync(player.UserId, Cash.Value)
	Cash.Changed:connect(function()
		ds:SetAsync(player.UserId, Cash.Value)
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.BloodBucks.Value)
end)

This script needs to be modified so that it saves the children of the leaderstats folder, not just one value.

When the player joins, you want to do GetAsync(), as you are getting the data asynchronously to the SetAsync.
So, basically, GetAsync will get the data, while SetAsync will save the data.
I’d also suggest you create the “Cash” inside the script.

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
local Players = game.Players

Players.PlayerAdded:Connect(function(player)
	local Cash = Instance.new("IntValue", player)
	Cash.Name = "CashValue"
	
	
	local PlayerID = "Player:"..player.UserId
	local CashData
	local success, errormessage = pcall(function()
		CashData = ds:GetAsync(PlayerID)
	end)
	if success then
		player.PlayerGui:WaitForChild("Cash")
		player.CashValue.Value = CashData --Gets the data defined below
	else
		print("There has been an error loading the player's cash.")
		warn(errormessage)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local PlayerID = "Player:"..player.UserId
	local CashData = player.CashValue.Value
	local success, errormessage = pcall(function()
		ds:SetAsync(PlayerID, CashData) -- Saves the data
	end)
	if success then
		print("Cash Data saved successfully!")
	else
		print(There has been an error saving the player's cash!)
		warn(errormessage)
	end
end)

You also want a separate script that has a GUI displaying the amount of Cash a player has

In case you want the coins to be displayed in the leaderboard, instead of the GUI, just make the Cash be in the leaderboard, edit the code in a way it fits your goal with the “Cash” value for your game.

How do I save the child values of the leaderstats folder? Not just one value, all of them.

To make it clear, let’s say I make a new value: Gems. I shouldn’t have to edit the save script because it saves the children of leaderstats, not just specific values.

For that, you will be using a table, instead of a single variable, then use that table on the DataStore Script.

So this is the script?

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
local Players = game.Players
Players.PlayerRemoving:Connect(function(player)
	local PlayerID = "Player:"..player.UserId
	local CashData = {player.leaderstats.BloodBucks.Value, player.leaderstats.Upgrade.Value, player.leaderstats.Gems.Value}
	local success, errormessage = pcall(function()
		ds:SetAsync(PlayerID, CashData) -- Saves the data
	end)
	if success then
		print("Cash Data saved successfully!")
	else
		print("There has been an error saving the player's cash!")
			warn(errormessage)
	end
end)