How to drop your left amount of money

Hi i am doing a drop cash system and i was wondering how could i implement the drop cash left if your cash is lower than the Desired Cash to drop (cashNum), because, let’s say i have 2 money, and i want to drop 20 cash, the cash it will drop just 20, but it isn’t correct, it would drop 2 instead, or in any case, just say “You don’t have that many cash” , how would i do it ?

drop_cash.OnServerEvent:Connect(function(player: Player, cashNum: number)
	
	local character = player.Character
	
	if playerTable[player.Name] then
		print(playerTable)
		if tick() - playerTable[player.Name] > cooldownTimeDrop then
			local leaderstatsFolder = nil
			local playerstats = nil
			if player:FindFirstChild(Settings["playerstats_Name"]) then
				playerstats = player:WaitForChild(Settings["playerstats_Name"]) :: Folder
			end
			if player:FindFirstChild(Settings["leaderstats_Name"]) then
				leaderstatsFolder = player:WaitForChild(Settings["leaderstats_Name"]) :: Folder
			end
			if leaderstatsFolder and playerstats then
				local Cash = playerstats:WaitForChild(Settings["currency_Name"]) :: NumberValue
				local StoredCash = playerstats:WaitForChild(Settings["storedcurrency_Name"]) :: NumberValue
				if Cash and StoredCash then
					
					if Cash.Value >= 1 then
						playerTable[player.Name] = tick()
						Cash.Value = Cash.Value
						task.wait()	
						local cashToGive = cashNum
							
						if Cash.Value <= cashToGive then
						--	warn(cashNum - Cash.Value)
							Cash.Value = 0
						elseif Cash.Value >= cashToGive then
							Cash.Value -= cashToGive
						elseif cashToGive > Cash.Value then
							
						end
						
						local cashclone = cash_template:Clone()
						local billboard = cashclone:WaitForChild("BillboardGui")
						local amount_text = billboard:WaitForChild("amount")
						cashclone.CollisionGroup = "Cash"
						local amount = cashclone:WaitForChild("amount") :: NumberValue
						local Ownership_plr = cashclone:WaitForChild("Ownership") :: ObjectValue
						cashclone.Anchored = true
						local modelgroup = Instance.new("Model")
						modelgroup.Name = player.Name.."'s ".. "Cash"
						cashclone.Parent = modelgroup
						Ownership_plr.Value = player
						task.wait()
					
						amount.Value = cashToGive
						amount_text.Text = tostring(amount.Value.."$")

When dropping cash you can do something like this:

local Amount = 20 -- this is the amount of cash they want to drop
local Money = 5 -- the amount of money they have

local AmountToDrop = math.min(Money,Amount) -- Picks the lowest number, so if they have 5 dollars, its going to drop 5 dollars, if they have 500, it'll drop 20

If you care for the docs on math.min

2 Likes

You’re so fast to answer now… why do I see you everywhere ???

3 Likes

before you drop the 20 cash make an if statement that checks if the player has 20+ cash. if they do not, you can create a variable with their cash amount, and set their money to 0. With this variable that holds the cash amount, you can then drop that amount

2 Likes

Im a professional at being everywhere at once :]

3 Likes

This would indeed work, but instead of doing all of those if statements you can shorten it to just using math.min, its much more efficient and will save you lines of code!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.