Cooldown going way to fast

Hello, I’m making a robbery system but after I rob it 3 time’s the cooldown gets way to fast

here’s a video

robloxapp-20220612-1619282.wmv (731.9 KB)

the cooldown function

local CoolDownTime = 12
local isCoolDown = false

local function CoolDown()
	isCoolDown = true
	
	while task.wait(1) do
		if CoolDownTime >= 2 and isCoolDown == true then
			--code
			CoolDownTime -= 1
		elseif CoolDownTime <= 1 then
     --code
		end
	end
end

every time you rob it the cooldown function gets called

Have you tried to increase the delay here?

Also, instead of a while loop -

Why not simply use a for loop?

3 Likes

You need to end the loop when the countdown is over. Take the task.wait out of the while loop declaration and replace it with a condition to end the loop when the countdown is over

2 Likes

Yup like he said, the variable called isCoolDown is used only to check the loop is running which makes it redundant. This is probably all you need, check isCoolDown == false to allow people to rob, i.e…

local function CoolDown()
	isCoolDown = true;
	wait(CoolDownTime);
	isCoolDown = false;
end
1 Like

Just so you’re aware, this code is going to lead to a memory leak, a new ‘while true do’ loop is began each time the ‘CoolDown’ function is called, remember to explicitly break out of loops via a ‘break’ statement or use a condition which can be false (task.wait(1) will always be considered truthy).

1 Like

Yeah quite right, but I did say though in my answer. You only call this function when isCoolDown == false so you will not get any overlaps in CoolDown() being called. Provided the logic that leads to this function being called (i.e. a Remote or whatever that checks isCoolDown==false which then executes the CoolDown function) there should be no multiple instances of the function being called. The while true do loop is gone and there is no need for it when all you are doing is waiting for a countdown to expire. If there was some Gui Text displaying the countdown then you would need the while true do to adjust the text every second, but since it is simply a countdown there is nothing to do but wait until it finishes.

1 Like