How to not make my function spammable

Alright basically I’m working on a magic game and the magic skills are tools. Once the player uses a skill, it’s supposed to give the player a little bit of EXP and then they would need to wait until the cooldown on their skill is done so they can use that move again to gain more EXP. However my local script would give EXP every click and I don’t know how to fix it.

Here’s the full local script:

local UserInputService = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer
local Camera = workspace.Camera
local Mouse = player:GetMouse()

local tool = script.Parent
local AnimationsFolder = tool:WaitForChild("AnimationsFolder")
local Remotes = tool.Remotes
local CanAttack = tool:WaitForChild("CanAttack")
local SwordOut = tool:WaitForChild("SwordOut")


local Exp_1 = false


tool.Equipped:Connect(function()
	Remotes:WaitForChild("SkillEqRE"):FireServer(tool,true)
end)



tool.Unequipped:Connect(function()
	Remotes:WaitForChild("SkillEqRE"):FireServer(tool,false)
end)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Events:WaitForChild("AddExp")

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			spawn(function()
				Exp_1 = true
				Exp_1 = Remotes:WaitForChild("AddExp"):InvokeServer()
			end)
		end
	end)
end)

Main problem is here:

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			spawn(function()
				Exp_1 = true
				Exp_1 = Remotes:WaitForChild("AddExp"):InvokeServer()
			end)
		end
	end)
end)

It basically invokes an event every click and I need to fix this.

You can use a debounce


local debounce = false

if debounce == false then
debounce = true
--Do stuff
debounce = false
else
--script is already running
end
1 Like

How would I add that? I’m a bit confused.

You’ll need to add what’s called a debounce.

The first thing we’re going to want to do is to remove this connection from the inside of “Equipped” because otherwise, it’ll cause a memory leak.

We can also remove the spawn() because there is no reason to create a new coroutine in this case.

local ToolEquipped = false
tool.Equipped:Connect(function(mouse)
	ToolEquipped = true
end)
tool.Unequipped:Connect(function(mouse)
	ToolEquipped = false
end)
local Debounce = false
mouse.Button1Down:Connect(function()
	if ToolEquipped and mouse.Target and mouse.Target.Parent and Debounce == false then
        Debounce = true -- Set to true so this cant fire again until variable is false again.
		Exp_1 = true
		Exp_1 = Remotes:WaitForChild("AddExp"):InvokeServer()
        wait(1) -- Assuming cooldowntime is 1
        Debounce = false
	end
end)
2 Likes