How do i save coins on a datastore2 script?

So i want to save coins but im not sure how to, can someone help me?
when i try making the coins a seperate value in the same folder the “replicateddatafolder” it doesn’t save so i just deleted it. How do i put the coins value inside the “inventorystring” and then make it save?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")

local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))


game.Players.PlayerAdded:Connect(function(player)
	local InventoryStore = DataStore2("Inventory", player)
	
	local replicatedDataFolder = Instance.new("Folder")
	replicatedDataFolder.Parent = ReplicatedStorage.ReplicatedData
	replicatedDataFolder.Name = player.UserId
	
	local inventoryString = Instance.new("StringValue")
	inventoryString.Parent = replicatedDataFolder
	inventoryString.Name = "Inventory"
	
	local inventoryData = InventoryStore:Get({})
	inventoryString.Value = HttpService:JSONEncode(inventoryData)
	
	InventoryStore:OnUpdate(function(decodedData)
		inventoryString.Value = HttpService:JSONEncode(decodedData)
		
		
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local ReplicatedData = ReplicatedStorage.ReplicatedData:FindFirstChild(player.UserId)
	
	if (ReplicatedData) then
		ReplicatedData:Destroy()
	end
end)

Not sure if read about using DataStore2. First of all, this module automatically caches data so there’s no point in creating another place to display data. Secondly, for this module, the data is meant to be updated when something gets changed, this means saving the data when the player leaves is incorrect.

To access the data inside a client script, one possible way is to fire a remote to the client when OnUpdate fires. The local script will then recieve the updated data and save it as local cache inside the script itself or otherwise.

So i don’t need a PlayerRemoving?

You don’t need a playerremoving event. DataStore2 will automatically save the data for you when the player leaves.

All you really need to do to save coins would be to create a datastore for the player

local InventoryStore = DataStore2("Inventory", player)
InventoryStore:Get({})

-- when something happens, use :Set or :Update to change the data

-- :Set
InventoryStore:Set({food = 10, stick = 6})

-- :Update
InventoryStore:Update(function(oldData)
    oldData.food += 6
    oldData.stick += 3
    return oldData
end)

ive figured out how to use the coins but im not sure how to make the coins visible on a textlabel

local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage.RemoteEvents.UpdateClientCurrency.OnClientEvent:Connect(function(amount)
	script.Parent.Text = "$" .. amount
end)

You can use OnUpdate for a callback

CoinStore:OnUpdate(function(coins)
    updateClientCurrency:FireClient(player, coins)
end)

FYI: you are able to save tables, there is no need to use json

would i still need to do script.Parent.text?

The code I provided was pseudocode that needs to be used on the server. Handling UI elements should be done on the client.

i already did a fire client on here a serverscript

local function updateClientCurrency(amount)
			replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
		end
		
		updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
		
		CurrencyStore:OnUpdate(updateClientCurrency)