Cooldown not functioning properly

I’ve got the weirdest issue going on with my cooldown statement. The issue is when you press “F” it is suppose to fire something once which it does, but only the first time. After you unequip the tool then re-equip it, it runs another time. So basically it will run once, (unequips then reequips) it runs twice, (unequips then reequips) it runs three times, and so on. I’ve got no clue why this is happening so I hope one of you can help me figure it out.

My Code:

local InputService = game:GetService("UserInputService")

local Tool = script.Parent.Parent
local CoolDown = false
local Equipped = false
local Aiming = false
local Loaded = false
local Plr = game.Players.LocalPlayer




Tool.Equipped:Connect(function()
	Equipped = true
	
	-- Animations
	local Idle = Tool.Assets.Animations.IdleAnimation
	IdleAnim = Plr.Character.Humanoid:WaitForChild("Animator", 3):LoadAnimation(Idle)
	local Aim = Tool.Assets.Animations.AimAnimation
	AimAnim = Plr.Character.Humanoid:WaitForChild("Animator", 3):LoadAnimation(Aim)
	local Load = Tool.Assets.Animations.LoadAnimation
	LoadAnim = Plr.Character.Humanoid:WaitForChild("Animator", 3):LoadAnimation(Load)
	
	
	if Equipped then
		IdleAnim:Play()
	end
	
	InputService.InputBegan:Connect(function(Input, Received)
		if Input.UserInputType == Enum.UserInputType.MouseButton1 and not CoolDown and Equipped then
			CoolDown = true
			
			print("Clicked")
			
			CoolDown = false
			
		elseif Input.UserInputType == Enum.UserInputType.Keyboard then
			if Input.KeyCode == Enum.KeyCode.F and Equipped and not CoolDown then
				CoolDown = true

				print("Pressed F")
					
				if not Loaded then		
					IdleAnim:Stop()
						
					LoadAnim:Play()
					LoadAnim.Stopped:Wait()
					Loaded = true
				end
					
				if Loaded then
					if not Aiming then
						IdleAnim:Stop()
						Aiming = true
						AimAnim:Play()

					elseif Aiming then
						AimAnim:Stop()
						Aiming = false
						IdleAnim:Play()
							
					end
				end
					
				CoolDown = false
				
			end
		end
	end)
end)



Tool.Unequipped:Connect(function()
	IdleAnim:Stop()
	AimAnim:Stop()
	LoadAnim:Stop()
	Aiming = false
	CoolDown = false
	Equipped = false
end)

Edit: Sorry forgot to mention the portion that is runs again and again is where that, “Pressed F” print statement is located.

It’s probably because you have no code that disconnects your Inputbegan event when the tool is unequipped. Create a variable that’s empty at first and make it contain your InputBegan connection so at the end when you unequip the tool, it disconnects it.

Try this out

local InputService = game:GetService("UserInputService")

local Tool = script.Parent.Parent
local CoolDown = false
local Equipped = false
local Aiming = false
local Loaded = false
local Plr = game.Players.LocalPlayer

local connection

Tool.Equipped:Connect(function()
	Equipped = true
	
	-- Animations
	local Idle = Tool.Assets.Animations.IdleAnimation
	IdleAnim = Plr.Character.Humanoid:WaitForChild("Animator", 3):LoadAnimation(Idle)
	local Aim = Tool.Assets.Animations.AimAnimation
	AimAnim = Plr.Character.Humanoid:WaitForChild("Animator", 3):LoadAnimation(Aim)
	local Load = Tool.Assets.Animations.LoadAnimation
	LoadAnim = Plr.Character.Humanoid:WaitForChild("Animator", 3):LoadAnimation(Load)
	
	
	if Equipped then
		IdleAnim:Play()
	end
	
	connection = InputService.InputBegan:Connect(function(Input, Received)
		if Input.UserInputType == Enum.UserInputType.MouseButton1 and not CoolDown and Equipped then
			CoolDown = true
			
			print("Clicked")
			
			CoolDown = false
			
		elseif Input.UserInputType == Enum.UserInputType.Keyboard then
			if Input.KeyCode == Enum.KeyCode.F and Equipped and not CoolDown then
				CoolDown = true

				print("Pressed F")
					
				if not Loaded then		
					IdleAnim:Stop()
						
					LoadAnim:Play()
					LoadAnim.Stopped:Wait()
					Loaded = true
				end
					
				if Loaded then
					if not Aiming then
						IdleAnim:Stop()
						Aiming = true
						AimAnim:Play()

					elseif Aiming then
						AimAnim:Stop()
						Aiming = false
						IdleAnim:Play()
							
					end
				end
					
				CoolDown = false
				
			end
		end
	end)
end)



Tool.Unequipped:Connect(function()
	IdleAnim:Stop()
	AimAnim:Stop()
	LoadAnim:Stop()
	Aiming = false
	CoolDown = false
	Equipped = false
	if connection then connection:Disconnect() end
end)

Oh, thank you. It is now working perfectly. I haven’t used the UserInputService much at all so I didn’t know I needed to do that.

1 Like

Anytime! if you have anymore issues don’t be afraid to make another post!

And disconnecting events can prove more uses than that, you’ll likely see more uses for it when you come across a similar issue!

1 Like

No offense but did you read this post at all? That other post has near nothing to do with this.

1 Like