Tool:Deactivate() messing with Tool.Deactivated event

hey, I was making a tool ability today, but I realized that if you deactivate the tool from another line it will fire the Tool.Deactivated event twice if you release your finger off your mouse. Is there any way to fix this?

source code:

local tool = script.Parent
local enabled = false
local start = nil
local duration = 3

tool.Activated:Connect(function()
  print "activated"
  enabled = true
  start = os.clock()

  while enabled == true do
    if os.clock() - start >= duration then
      tool:Deactivate()
    end
  end

end

tool.Deactivated:Connect(function()
  print "deactivated"
end)

If you have any solutions, please share! Thanks in advance :slight_smile:

have you tried using a debounce/check each time you deactivate it?

1 Like

oh, I haven’t thought about that, thanks!

1 Like

hey, I wanted to jump back on this, it’s not really working with debounce, because if I use the :Deactivate() method on the tool while the player is still holding it, they can keep holding it until the debounce is finished and then released, therefore triggering the .Deactivated event.

1 Like

Yes, you can fix this issue by adding a check inside the while loop of your Activated event callback, to see if the tool is still enabled before calling Deactivate(). You can do this by adding a new if statement inside the while block, like this:

while enabled == true do
  if os.clock() - start >= duration then
    if enabled == true then
      tool:Deactivate()
    end
  end
end