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)

players.PlayerAdded:Connect(function(player)
	local CashStore = DataStore2("Cash", player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

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

	UpdateClientCurrency(CashStore:Get(0))

	CashStore:OnUpdate(UpdateClientCurrency)
end)

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

local cap = ... -- Your cap here

if amount > cap then 
    amount = cap 
    CashStore:Set(cap )
end
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
end