Tool.Equipped keeps working after unequipped

Hi everyone. I have this script in a localscript:

glove.Equipped:Connect(function()
	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if debounce == false then
				debounce = true
				RemoteGloveEvent:FireServer(mouse.Hit, camera.CFrame.LookVector)
				wait(2)
				debounce = false
			end
		end
	end)
end)

The script works fine and the event is fired on MouseClick once the tool is equipped. The issue is that, once I unequip it or equip other tools, the event keeps being fired whenever I click.

Nothing happens when I click before the tool is equipped for the first time, but once it is equipped it seems to be firing the event constantly.

I tried using the unequip function but I am not sure what to do in the function.
glove.Equipped:Connect(function()
	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if debounce == false then
				debounce = true
				RemoteGloveEvent:FireServer(mouse.Hit, camera.CFrame.LookVector)
				wait(2)
				debounce = false
			end
                      else 
                      end
		end
	end)
end)

glove.Equipped:Connect(function()
	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if debounce == false then
				debounce = true
				RemoteGloveEvent:FireServer(mouse.Hit, camera.CFrame.LookVector)
				wait(2)
				debounce = false
			end
           else 
          glove.Equipped = false
          end
		end
	end)
end)

Try to do this, it should disable the IsEquipped status.

I just tried, it still does the same

Just use tool.Activated instead of tool.Equipped and MouseButton1

1 Like

This will cause a memory leak. Set your connect event to a variable, set that variable to nil one line before that (otherwise it won’t be recognized inside the function) and inside the function do the same, but with glove.Unequipped.

glove.Equipped:Connect(function()
    local inputBeganEvent = nil
	inputBeganEvent = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if debounce == false then
				debounce = true
				RemoteGloveEvent:FireServer(mouse.Hit, camera.CFrame.LookVector)
				wait(2)
				debounce = false
			end
		end
	end)
        
    local gloveUnequippedEvent = nil
    gloveUnequippedEvent = glove.Unequipped:Connect(function()
        inputBeganEvent:Disconnect()
        gloveUnequippedEvent:Disconnect()
    end)
end)

This solution is overcomplicated and I don’t recommend it when you can just use what @WoTrox suggested. This way the tool will be activated by left clicking and touching on mobile without the input checks, is equipped checks and worries about memory leaks.

1 Like

I see, thanks for the information.

That seemed to do the trick. Thanks a lot!

Thank you, this is helpful. I used Wo Tox’s answer ( use tool.Activated instead of tool.Equipped and MouseButton1) before I saw yours. It is working, but do you think doing it wiht “Activated” might cause other problems?

No, usually .Activated is superior when you want to use left click and touch on mobile. It will cause problems only when you want the input to be something else in which case you must use my solution and change the input you’re looking for.

1 Like