Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I add a cooldown to this function?

Hello! I'm having an issue with adding a cool down to a weapon I made. The goal of the script below is to play an animation and a blast sound whenever the weapon is fired. But I'm trying to add a 2 second cool down to this because the weapon itself has a 2 second reload time and I want the animation and blast sound to match the reload time. But I'm having a hard time implementing this cooldown.

Here's the script below:

local Tool = script.Parent
local AnimationR15 = script:WaitForChild("AnimationR15", 1)
local AnimationR6 = script:WaitForChild("AnimationR6", 1)

local cooldown = 2 -- how long the cooldown will take
local GunSound = game.Workspace.GunBlast

Tool.Activated:Connect(function()
	local Character = Tool.Parent
	if Character then
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		if Humanoid then
			local LoadedAnim = nil
			if Humanoid.RigType == Enum.HumanoidRigType.R15 and AnimationR15 then
				LoadedAnim = Humanoid:LoadAnimation(AnimationR15)
			elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and AnimationR6 then
				LoadedAnim = Humanoid:LoadAnimation(AnimationR6)
			end
			if LoadedAnim then
				LoadedAnim:Play()
				GunSound:Play()
				
				wait(cooldown) -- attempting to play the cool down after the gun is shot
			end
		end
	end
end)

Any help is appreciated! Thanks :)

2 answers

i am honestly terrible a scripting, so where is what you can do:

A. Use Debounce like a normal person

B. My method

basically ur problem is that u are just waiting after your gun is fired, but the computer doesn't know that u cant do anything when the wait is in progress, so here is what you can do.

local Usable = true
local Tool = script.Parent
local AnimationR15 = script:WaitForChild("AnimationR15", 1)
local AnimationR6 = script:WaitForChild("AnimationR6", 1)
local GunSound = game.Workspace.GunBlast

Tool.Activated:Connect(function()
	if Usable == true then
		Usable = false
		--yada yada script
		wait(2)
		Usable = true
	end
end)

this should work but idk, hopes it helps 'cause im terrible compared to u.

2 comments

0

Isn't this method basically a debounce?

0

This didn't quite work, but I ended up using debounce and got it working! Thanks for getting me on the right track though!

Got it working! I ended up implementing debounce in the function and that seems to do the trick. Thanks to JaydenLegenderyDude for getting me on the right track!

Here's the code if anyone wants to see it:

local Tool = script.Parent
local AnimationR15 = script:WaitForChild("AnimationR15", 1)
local AnimationR6 = script:WaitForChild("AnimationR6", 1)

local debounce = false
local cooldown = 2
local GunSound = game.Workspace.GunBlast

Tool.Activated:Connect(function()
	if debounce then return end 
	debounce = true 
	
	local Character = Tool.Parent
	if Character then
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		if Humanoid then
			local LoadedAnim = nil
			if Humanoid.RigType == Enum.HumanoidRigType.R15 and AnimationR15 then
				LoadedAnim = Humanoid:LoadAnimation(AnimationR15)
			elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and AnimationR6 then
				LoadedAnim = Humanoid:LoadAnimation(AnimationR6)
			end
			if LoadedAnim then
				LoadedAnim:Play()
				GunSound:Play()
				wait(cooldown)
				
				debounce = false
					
			end
		end
	end
end)

God bless and have a great day :)

This discussion is preserved as it was. New answers, comments, and votes are closed.