Simple script - touched event with activated event. issues/buggy?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I only want the touched event to occur once the tools been activated…

  2. What is the issue? Include screenshots / videos if possible!
    Even though the touched function is inside the activated function, the print statement still spams when it’s been touched even if the tool hasn’t been activated. It is to my knowledge that Tool.Activated only occurs when the player clicks.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Looked around and couldn’t find anything relevant, and this seems rather “simple”

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

tool = script.Parent
debounce = false
enemy = game.Workspace.Dummy

tool.Activated:Connect(function() -- Once the tool is activated then it connects the function below.
	tool.Sword.Touched:Connect(function(hit) -- Should only happen when tool.Activated happens with mouse click?
		if hit.Parent == enemy then
			if debounce == false then
				debounce = true
				print("Enemy hit")
				wait(.04)
				debounce = false
			end
		end
	end)
end)
1 Like

If the problem is that it keeps detecting touches after the attack has ended, maybe this will work.

local tool = script.Parent
local debounce = false
local enemy = game.Workspace.Dummy
local touchConn

tool.Activated:Connect(function() -- Once the tool is activated then it connects the function below.
	if touchConn then
		touchConn:Disconnect()
		touchConn = nil
	end
	touchConn = tool.Sword.Touched:Connect(function(hit) -- Should only happen when tool.Activated happens with mouse click?
		if hit.Parent == enemy then
			if debounce == false then
				debounce = true
				print("Enemy hit")
				wait(.04)
				debounce = false
				if touchConn then
					touchConn:Disconnect()
					touchConn = nil
				end
			end
		end
	end)
end)

looks like it works! Had no idea about the disconnect event! TY :slight_smile: