How to add a cooldown to Donate GUI

Is it possible to add a cool down to my donation system? I don’t want people, namely exploiters, to be able to donate to people too fast. I want there to be a 5 minute cool down in between. Maybe a donation limit as well.

Code
SendMoney.SendButton.MouseButton1Click:Connect(function()
		Sounds.ButtonClick:Play()
		local amount = tonumber(SendMoney.AmountBox.Text)
		if amount then
			local otherPlayer = PlayersService:FindFirstChild(SendMoney.PlayerBox.Text)
			if otherPlayer then
				if otherPlayer == Player then print('You cant send money to yourself') return end
				if PlayerData.Drachma >= amount then
					Events.SendMoney:FireServer(amount,otherPlayer)
				else
					print('Sorry, you do not have enough money!')
				end
			else
				print('Sorry"'..SendMoney.PlayerBox.Text..'" Is not a valid player!')
			end
		else
			print('Sorry, You must enter in a valid number to send! "'.. SendMoney.AmountBox.Text .."' Is not a number!")
		end
	end)

Okay. I’ve had this issue before. What I do is have a function to call the cooldown, and a variable to see if the cooldown is active. Here’s an example.

local Debounce = false
function CoolDown()
	wait(60)
	Debounce = false
end

Instance.Event:Connect(function()
	if Debounce == true then
		print("wait, stop, stop, you cant do that!!!")
	else
		Debounce = true
		-- insert ya code here
		CoolDown()
	end
end)

I use this for tool cooldowns, so I’m not sure how well it will work in your case.

Cooldowns like this are called debounce, i think.

1 Like

For a cooldown you could use timestamps and check if the elapsed time is greater than the time the last donation was sent + the cooldown. For a donation limit you can increment a variable each time the player donates and check if the donations limit has been reached. For example:

local donationCooldown = 300 -- 5 minutes in seconds
local donationLimit = 5
local lastDonationTS = -donationCooldown -- This is the cooldown but negative so the player doesn’t have to wait to donate after joining
local donationCount = 0
local function donate()
    if time() >= lastDonation + donationCooldown and donationCount < donationLimit then
        lastDonationTS = time()
        donationCount += 1
        -- donate
    end
end
1 Like