Capping a Datastore2

Hello everyone! I have looked but I cannot find anything on how to cap a Datastore2’s amount. I want to cap money at 999 million but it is not working. Please help!

local players = game:GetService("Players")

local DataStore2 = require(1936396537)

	local CashStore = DataStore2("Cash", player)

	local leaderstats ="Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Cash ="IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = 0
	local function UpdateClientCurrency(amount)
		Cash.Value = amount



Try adding an “if” inside the Update function like this

local cap = ... -- Your cap here

if amount > cap then 
    amount = cap 
    CashStore:Set(cap )
1 Like

Would it go before or after the Cash.Value? It would be before, right?

edit: it worked, thank you so much! I had to make it so it also set the Cash.Value to cap but otherwise it worked!

local function UpdateClientCurrency(amount)
-- Should go here so the variable "amount" changes before the cash.value --
Cash.Value = amount