How to stop a touched event

(Sorry for bad english lol )
I want to to activate the touch function when the tool is clicked
but when i do it still stays activated even when i click somewhere else. I need it to work with the (Tool) activated function
I have tried using debounce but still i have the same problem.

local event = script.Parent:WaitForChild("Folder"):WaitForChild("bonk")
event.OnServerEvent:Connect(function(player)


	script.Parent.Activated:Connect(function()
    local animation = player.Character.Humanoid:LoadAnimation(script.Parent.Animation)
	animation:Play()

		local touch	
	     touch =  script.Parent.Handle.Touched:Connect(function(hit)
			
				if hit.Parent:FindFirstChildOfClass("Humanoid") then
					print("hellu")
				
	     	touch:Disconnect()
				end
			


	end)
	end)
	end)


4 Likes

First thing: you want to load your animation only once, so do not load it in your function, load it at the top of your script.

Second Thing: Humanoid:LoadAnimation is deprecated, so you should NOT use it. Instead, use the Animator instance, which should be a child of the Humanoid, if you havent done any changes to the default animation script.

And third, please use variables for stuff like the Tool/Handle/etc.

Instead of connecting and disconnecting a function to the Touched event each time you activate the tool, a better solution would be to connect a function to the Activated event which set a variable toolActivated to true, wait the lenght of the animation, then set this variable back to false. Also connect a function to the Touched event of your tool (and never disconnect it) which will verify if toolActivated is true, if it is, do something.

Something like this:

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")

local bonkAnimTrack = Animator:LoadAnimation(Tool:WaitForChild("Animation"))

local activated = false

Tool.Activated:Connect(function()
    activated = true
    wait(bonkAnimTrack.Lenght)
    activated = false
end)

Handle.Touched:Connect(function(hit)
    if not activated then
        return
    end
    activated = false -- to make sure the function doesnt run more than once per click

    -- do something here
end)
8 Likes

Thank you , sir. The scripts works perfectly fine now. =]

1 Like

Even though you already received a great solution, I will slide this post in here anyway since I spent too much time writing this:

It looks like you handle animations and target detection on the server, which most developers probably wouldn’t recommend. Regardless, it looks like you connect to the handle’s Touched event indefinitely once the player activates the tool. I recommend listening for both Activated and Touched independently, then only consider the result(s) of Touched when Activated was invoked recently. Here is how I would probably go about this:

local last; --the last time the tool was activated
local registerTime; --how long the window of time is to accept any collisions with the handle as an attack against a player
local tool; --reference to tool object
local handle; --reference to tool handle object
local event; --reference to remoteevent object that communicates to server for damage and verification

local hums = {}; --cache for every humanoid we sent to the server so we cant hit the same person twice with one activation
handle.Touched:Connect(function(hit)
	if not last or tick() - last > registerTime or not hit.Parent then return; end --disregard the collision if the player didnt activate the tool within registerTime
	
	local hum = hit.Parent:FindFirstChildWhichIsA'Humanoid'; --find a humanoid in the parent of the limb hit
	if not hum or hum.Health <= 0 or table.find(hums, hum) then return; end --ensure there is a humanoid and that the humanoid isnt dead and that the humanoid isnt already cached or else abort the operation
	
	hums[#hums + 1] = hum; --cache the humanoid
	event:FireServer(hum); --send the humanoid to the server so it can deal the damage. make sure it also verifies the hit was actually possible with a distance check
end)

tool.Activated:Connect(function()
	hums = {}; --empty the cache because this is a new activation
	last = tick(); --refresh the variable representing the last time the tool was activated
end)

This method is capable of handling multiple targets with one swing of what appears to be a baseball bat, if that is more suitable.

5 Likes

Thank you for you time, I’ll surely try this too =]

1 Like