Debounce Not Working?

I’m trying to make a Spiderman-like grappler and I want to add a debounce time so if you have an auto-clicker, the console doesn’t spam a bunch of errors which may reduce performance and plus, does a web shooting from your wrist every millisecond sound very realistic to you (lol)?

Don’t mind the fact that this is exactly 200 lines (unusual for me lol), just know that the tool.Activated:Connect(function() is the only code that matters.

Module Script: Client is the problem

-- Services

local RS = game:GetService('RunService')
local TS = game:GetService('TweenService')
local UIS = game:GetService('UserInputService')
local SG = game.StarterGui
local plrs = game.Players

-- Objects

local tool = script.Parent
local base = tool:WaitForChild('Base')
local spring = base.Spring
local hook = tool:WaitForChild('Hook')
local hookR = tool:WaitForChild('HookReference')
local hookW = hook.HookWeld

-- Technicals

local messageEv = script:WaitForChild('Message')
local activeBool = script:WaitForChild('Active')
local wasActive = false
local debounce = false
local debounceT = 0.5

-- Customization

local web = base.Web
local hookC = hook.Cursor3D
local attachSound = script.Attach
local breakSound = script.Break

local plr = nil
local m = nil -- Mouse

-- Current

local currentSKJ = nil -- current Safe Keeping Joint
local currentAJ = nil -- current Arm Joint
local currentT = nil -- current Tween

local GG = {} -- Module

function clientInit()
	plr = plrs.LocalPlayer
	m = plr:GetMouse()
	m.TargetFilter = plr.Character
	
	RS.RenderStepped:Connect(function()
		for i, v in pairs(plr.Character:GetChildren()) do
			if string.match(v.Name, 'Arm') or string.match(v.Name, 'Hand') or string.match(v.Name, 'Leg') or string.match(v.Name, 'Foot') then
				v.LocalTransparencyModifier = 0
			end
		end
	end)
	
	-- Problem Start
	
	tool.Activated:Connect(function()
		local initialPos = hookR.Position
		local dist = (m.Hit.p - initialPos).Magnitude
		local char = workspace[plr.Name]
		local hum = char.Humanoid
		local shot = false
		
		print(debounce)
		
		if not GG.IsActive() and m.Target and dist < 300 and not debounce then
			debounce = true
			
			messageEv:FireServer(true, m.Hit.p, dist)
			
			if hum.FloorMaterial == Enum.Material.Air then
				hum:ChangeState(Enum.HumanoidStateType.Ragdoll)
			end
			
			SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
			UIS.MouseIconEnabled = false
			hookC.Enabled = true
			
			print('1')
			
			task.wait(debounceT)
			
			print('2')
			
			debounce = false
			shot = true
		end
		
		print(shot)
	end)
	
	-- Problem End

	tool.Deactivated:Connect(function()
		local char = workspace[plr.Name]
		local hum = char.Humanoid
		
		if debounce then
			messageEv:FireServer(false)
			
			if hum:GetState() == Enum.HumanoidStateType.Ragdoll then
				hum:ChangeState(Enum.HumanoidStateType.GettingUp)
			end
			
			SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
			UIS.MouseIconEnabled = true
			hookC.Enabled = false
		end
	end)
	
	tool.Equipped:Connect(function()
		m.Icon = 'rbxassetid://6879292858'
	end)
	
	tool.Unequipped:Connect(function()
		messageEv:FireServer(false)
		
		SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		m.Icon = ''
		UIS.MouseIconEnabled = true
		hookC.Enabled = false
	end)
end

function serverInit()
	messageEv.OnServerEvent:Connect(function(player, active, targetPos, dist)
		activeBool.Value = active
		
		if GG.IsActive() then
			GG.Fire(targetPos, dist)
		elseif wasActive then
			GG.Reset(player)
		end
	end)
end

function init()
	if RS:IsClient() then
		clientInit()
	elseif RS:IsServer() then
		serverInit()
	end
end

function GG.IsActive()
	return activeBool.Value
end

function GG.Fire(targetPos, dist)
	local initialPos = hookR.Position
	local TI = TweenInfo.new(dist / 35, Enum.EasingStyle.Quint)
	local tween = TS:Create(spring, TI, {FreeLength = dist / 25, Stiffness = math.clamp(dist * 2, 500, 2000)})
	local char = tool.Parent
	
	currentSKJ = tool.JointSafeKeeping.RagdollRightShoulder
	currentAJ = char.RightUpperArm.RightShoulder
	currentT = tween
	
	wasActive = true
	
	hookW.Enabled = false
	hook.Anchored = true
	
	wait()
	
	hook.Position = targetPos
	spring.FreeLength = dist
	web.Enabled = true
	web.TextureSpeed = dist / 50
	web.TextureLength = dist / 50
	
	currentAJ.Parent = tool.JointSafeKeeping
	currentSKJ.Parent = char.RightUpperArm
	currentSKJ.Attachment0 = char.RightUpperArm.RightShoulderRigAttachment
	currentSKJ.Attachment1 = char.UpperTorso.RightShoulderRigAttachment
	currentAJ.Part0 = nil
	currentAJ.Part1 = nil
	
	tween:Play()
	attachSound:Play()
end

function GG.Reset(player)
	local char = workspace[player.Name]
	
	currentSKJ = tool.JointSafeKeeping.RightShoulder
	currentAJ = char.RightUpperArm.RagdollRightShoulder
	
	wasActive = false
	
	if currentT then
		currentT:Cancel()
		breakSound:Play()
	end
	
	hook.CFrame = hookR.CFrame
	hookW.Enabled = true
	hook.Anchored = false
	spring.FreeLength = 1
	web.Enabled = false
	
	currentAJ.Parent = tool.JointSafeKeeping
	currentSKJ.Parent = char.RightUpperArm
	currentSKJ.Part0 = char.UpperTorso
	currentSKJ.Part1 = char.RightUpperArm
	currentAJ.Attachment0 = nil
	currentAJ.Attachment1 = nil
end

init()

return GG

Thank you in advance! :slight_smile: If you need more help, just let me know!

What isn’t working? When you spam, does it just keep shooting the grappler?

A bunch of errors happen and it also might reduce performance.

Actually, it seems like the errors don’t even affect reusing the grappler again, just performance. But, a debounce would be more realistic anyway, and I’m trying to get that dynamic and realistic feel.

put parentheses around your if conditions, like (not GG.IsActive()) and (m.Target) and (dist < 300) and (not debounce).

plus, your debounce should be around the ENTIRE function, not just the part that makes it work.

Like this? (I know that I’m replying late :grimacing:)

	-- Problem Start
	
	if not debounce then -- do I put it here?
		tool.Activated:Connect(function()
			local initialPos = hookR.Position
			local dist = (m.Hit.p - initialPos).Magnitude
			local char = workspace[plr.Name]
			local hum = char.Humanoid
			local shot = false

			print(debounce)

			if (not GG.IsActive()) and (m.Target) and (dist < 300) then
				debounce = true

				messageEv:FireServer(true, m.Hit.p, dist)

				if hum.FloorMaterial == Enum.Material.Air then
					hum:ChangeState(Enum.HumanoidStateType.Ragdoll)
				end

				SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
				UIS.MouseIconEnabled = false
				hookC.Enabled = true

				print('1')

				task.wait(debounceT)

				print('2')

				debounce = false
				shot = true
			end

			print(shot)
		end)
	end

	-- Problem End

Does anyone have ideas? sdasfwafawfasfwafwa