Script Cooldown?

Hi i have this script for a block animation and i was wondering how i could make it so this script can only be activated every 3 seconds

local Values = require(game.ReplicatedStorage:WaitForChild("Combat"):WaitForChild("Values"))
script.Parent.OnServerEvent:Connect(function(plr,Action)
	local Char = plr.Character
	local Hum = Char:WaitForChild("Humanoid")
	if Action == "Start" then
		Values:CreateValue("BoolValue",Char,"Blocking",false,math.huge)
		Values:CreateValue("BoolValue",Char,"PB",false,.1)
		
		local BlockAnim = Hum:LoadAnimation(script:WaitForChild("BlockAnim"))
		BlockAnim:Play()
	end
	
	if Action == "Stop" then
		for i,v in pairs(Char:GetChildren()) do
			if v.Name == "Blocking" then
				v:Destroy()
			end
		end
		
		local AnimTracks = Hum:GetPlayingAnimationTracks()
		for i,v in pairs(AnimTracks) do
			if v.Name == "BlockAnim" then
				v:Stop()


			end
		end
	end
end)



How can I achieve a 3 second cooldown in this script?
To achieve this goal, you can use Debounce Patterns.

Here is some information about debounce patterns

Example

local debounce = false

function dothing()
	if not debounce then 
		debounce = true
		task.wait(3)
		debounce = false
	end
end

i dont want it to be when its used to many times i just want it so the player can only use that script every 3 seconds

Which is why you introduce debounce patterns…

man i cant spellllllllllllllll

As Xynterical Stated Debounce would be your solution. There is multiple Youtube tutorials so that Might help you learn about what your trying to fix.

1 Like

would it not be possible to just add a wait()

Yes it would be possible but use “Task.wait()”

where would i add task.wait in the script?

No, because it would cause delay and weird glitches. If you use task.wait and the script gets called again, it will create multiple tasks of one action.

i have no clue how to use debounce thats the thing

A debounce would be

local Check = False -- Debounce varible

Function()
 if not Check then -- Checks if the Varible Check is false
 Check = True -- Prevent the script to be called again
 Task.wait(3) -- CoolDown
 Check = False -- The Function Can be called again as check is now false
 end

end)

Please Note this can be for anything and not just functions.

where do i put this in my script start end or somewhere else

Any part that requires a Delay and all the code running in the Statement will be Delayed until The variable check becomes False again

i want the whole script to be completly DISABLED for 3 seconds then to be usable again

Just keep track of the time, pretty simple and does not require any sort of yielding to accomplish its goal.

local LastClick = os.time()
-- to keep track of the time since the function fired

function something()
    local Elapsed = os.time() - LastClick -- gets the difference in time
    if Elapsed < 5 then return end -- ends function if time has not reached
    -- 5 seconds
    -- fire something here
    LastClick = os.time() -- resets timer
end

This would be more performant, and use less resources compared to yielding an entire thread, plus its a very simple and fast alternative to yielding

would this work ?

module = {}

module.Block = function(Char,Health,Animation,ParryTime)
	local ParVal = Instance.new("BoolValue",Char)
	ParVal.Name = "Parry"
	game.Debris:AddItem(ParVal,ParryTime)
	
	Char:WaitForChild("Humanoid"):LoadAnimation(Animation):Play()
	
	local BlockVal = Instance.new("NumberValue",Char)
	BlockVal.Name = "Blocking"
	BlockVal.Value = Health
	
	delay(2,function()
		BlockVal:Destroy()
		print("Destroyed")
	end)
end

module.UnBlock = function(Char,AnimName)
	for i,v in pairs(Char:GetChildren()) do
		if v.Name == "Blocking" then
			v:Destroy()
		end
	end
	
	local Tracks = Char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()
	
	for i,v in pairs(Tracks) do
		if v.Name == AnimName then
			v:Stop()
			local LastClick = os.time()
			-- to keep track of the time since the function fired
			local function CheckTime()
				if os.time() - LastClick >= 1 then
					Char:WaitForChild("Humanoid"):LoadAnimation(v.Animation):Play()
					print("Played")
				end
			end
			
			while v.IsPlaying do
				CheckTime()
				wait(5.5)
			end
			
			break
		end
	end
	
end

return module




It should get the job done, ill say a bit unoptimized.

for some reason its still not working

Just like @Cairo said. Store the time when you first executed the function and have a conditional statement inside of that function to check weather or not that time has exceeded it’s cooldown time. If it’s not working then the reference you stored the time is not being cached properly.