Debounce problem

My DEBOUNCE is supposed to add a cooldown for this tool. BUT it does work sometimes but sometimes when I spam click my mouse the debounce does not work and keeps activating the tool.

SCRIPT:

local tool = script.Parent
local runservice = game:GetService("RunService")
local batarang = tool.Batarang
local replicatedstorage = game.ReplicatedStorage
local myeventsfolder = replicatedstorage.MYEVENTS
local combatevent = myeventsfolder.COMBAT
local debounce = tool.DEBOUNCE
local equipcamera = myeventsfolder.EQUIPCAMERA
local attribute = tool:SetAttribute("ForceMouseLock", true)
local originalbatarang = game.ReplicatedStorage.COMBATQUIPMENT.Batarang
local tweenservice = game:GetService("TweenService")
local currentcharacter = nil
local refilling = false
local activatedwaittime = false
local waitingtime = 3

tool.Equipped:Connect(function()
	toolequipped = true
	wait()
	batarang.Motor6D.Part0 = batarang
	batarang.Motor6D.Part1 = tool.Parent.RightHand
	batarang.Motor6D.C1 = CFrame.new(0, -.2, 0)
end)

tool.Unequipped:Connect(function()
	toolequipped = false
end)

tool.Activated:Connect(function()
	if debounce.Value == true then
		debounce.Value = false
		print(refilling)
		local currentvalue = script.Parent.CurrentValue
		local maxvalue = currentvalue.Parent.MaxValue
		local character = tool.Parent
		currentcharacter = character
		local humanoid:Humanoid = character.Humanoid
		local righthand = character.RightHand
		local animator:Animator = humanoid:FindFirstChild("Animator")
		local loopthread
		if animator then
			local loopthread2 = coroutine.wrap(function()
				while task.wait() do
					if currentvalue.Value == 6 and refilling == true then
						refilling = false
					end
				end
			end)()
			if currentvalue.Value == 0 then
				myeventsfolder.SKILLVALUE:FireClient(game.Players:GetPlayerFromCharacter(currentcharacter), true)
				loopthread = coroutine.wrap(function()
					refilling = true
					wait(1)
					repeat
						currentvalue.Value += 1
						wait(1)
					until currentvalue.Value == 6
				end)()
			elseif currentvalue.Value > 0 and refilling == false then
					print("Activated")
					myeventsfolder.SKILLVALUE:FireClient(game.Players:GetPlayerFromCharacter(currentcharacter), false)
					print(currentvalue.Value)
					if loopthread ~= nil then coroutine.yield(loopthread) end
					currentvalue.Value -= 1
					humanoid.WalkSpeed = 0
					local animationtrack = animator:LoadAnimation(game.ReplicatedStorage.ANIMATIONS.THROWBATARANG)
					animationtrack.Priority = Enum.AnimationPriority.Action4
					animationtrack:Play()
					combatevent:FireClient(game.Players:GetPlayerFromCharacter(tool.Parent))
					animationtrack.Stopped:Wait()
					local connection
					connection = combatevent.OnServerEvent:Connect(function(player, mouseposition)
						connection:Disconnect()
						humanoid.WalkSpeed = 16
						character.HumanoidRootPart.CFrame = CFrame.lookAt(character.HumanoidRootPart.Position, mouseposition, character.HumanoidRootPart.CFrame.UpVector)
						local clonedbatarang = originalbatarang:Clone()
						clonedbatarang.Parent = workspace
						clonedbatarang.Position = righthand.Position
						clonedbatarang.CanCollide = false
						local tweeninfo = TweenInfo.new(.4, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0)
						local createtween = tweenservice:Create(clonedbatarang, tweeninfo, {Position = mouseposition})
						createtween:Play()
						local runconnection
						runconnection = runservice.Stepped:Connect(function()
							clonedbatarang.CFrame = clonedbatarang.CFrame * CFrame.Angles(1, 0, 0)
						end)
						createtween.Completed:Connect(function()
							runconnection:Disconnect()
						end)
						local touchconnection
						touchconnection = clonedbatarang.Touched:Connect(function(otherbasepart)
							if otherbasepart.Parent:FindFirstChild("Humanoid") and otherbasepart.Parent.Name ~= character.Name then
								local enemy = otherbasepart.Parent
								local enemyhumanoid:Humanoid = enemy.Humanoid
								if otherbasepart.Name == "Head" then
									enemyhumanoid.Health -= enemyhumanoid.Health
									touchconnection:Disconnect()
								elseif otherbasepart.Name == "LeftLeg" or otherbasepart.Name == "RightLeg" or otherbasepart.Name == "LeftFoot" or otherbasepart.Name == "RightFoot" then
									enemyhumanoid.Health -=  10
									touchconnection:Disconnect()
								else
									enemyhumanoid.Health -= 30
									touchconnection:Disconnect()
								end
							end
						end)
					end)
				end
			end
		end
		wait(waitingtime)
	    debounce.Value = true
end)
wait(waitingtime)
	    debounce.Value = true
ā€Ž 

put this inside the thread

What thread? There are many threads here.

Nevermind. My mind is lost right now lol

and try putting debounce = false at the end of the thread too

Are you using a boolvalue, if so why?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.