Touched event still firing multiple times

Hi! So, the :Touched event still firing multiple times even after i put a Debounce.
There’s a way to fix that?

local Tool = script.Parent.Parent
local hitbox = Tool.Handle.HitBox

debounce = true

Tool.Activated:Connect(function()
	if debounce == true then
		debounce = false
	Tool.Handle.Material = Enum.Material.Neon
	Tool.Handle.BrickColor = BrickColor.new("Institutional white")
	hitbox.Touched:Connect(function(wow)
		if wow.Name == "HumanoidRootPart" then
			print(wow)
			local HRP = wow.Parent:FindFirstChild("HumanoidRootPart")
			local Humanoid = wow.Parent:FindFirstChild("Humanoid") 
			Humanoid.PlatformStand = true
			local BodyVelocity = Instance.new("BodyVelocity", HRP)
			BodyVelocity.Velocity = Vector3.new(math.huge,math.huge,math.huge)
			Tool.Handle.Particle.Enabled = true
			Tool.Handle.Particle2.Enabled = true
			Tool.Handle.lightning_strike:Play()
			
			local sound = script.Parent.idloveyou:Clone()
			sound.Parent = wow.Parent:FindFirstChild("HumanoidRootPart")
			sound.PlayOnRemove = true
			sound:Destroy()
				
			game.ReplicatedStorage.Remotes.Camera.SmallExplosion:FireAllClients()
			
			wait(1)
			Tool.Handle.Particle.Enabled = false
			Tool.Handle.Particle2.Enabled = false
			Tool.Handle.Material = Enum.Material.Wood
			Tool.Handle.BrickColor = BrickColor.new("Dirt brown")
				BodyVelocity:Destroy()
				Humanoid.PlatformStand = false
				wait(10.479)
					debounce = true
			Tool.Handle.Material = Enum.Material.Neon
					Tool.Handle.BrickColor = BrickColor.new("Institutional white")
				end
			end)
		end
			end)

Thanks. :heart:

1 Like

Haven’t looked at the whole script yet but you are creating a new .Touched event every time that tool is equipped. Consider moving that connection outside of Tool.Activated? Also, you’re using the debounce on Tool.Activated… not on hitbox.Touched. On another look, what do you want to achieve exactly?

I’m trying to achieve a one-time touched fire per 11 sec delay.

Mhm, and do you only want to check the touch if that tool has been activated? Or do you want to check if parts are touching it if the tool has been equipped? I think you want the latter but just confirming.

Only when the tool has been activated.

Try changing the debounce = true to local debounce = true.
Don’t have a clue on why it doesn’t work if this isn’t the issue.

Stuff with local and global variables though I don’t know how they actually work in lua lol

So sort of like a sword. Where you activate the tool and you check for all the parts that hit it?

Oh I think I know the problem.

The Activated event is creating a new Touched connection every time you use the tool.
To get around this you can use the debounce in reverse for a seperate touched connection outside of the activated connection.

1 Like

I check all parts that touch the HitBox, and i only get the HumanoidRootPart

Not sure if this is what you need but give it a try. I would consider raycasting over this method, but it might be what you’re looking for!

local Tool = script.Parent.Parent
local hitbox = Tool.Handle.HitBox
local debounce = false
local Cooldown = 10 --Change this to the cooldown value
local function GetTouchingParts(part)
	local Connection = part.Touched:Connect(function() end)
	local Parts = part:GetTouchingParts()
	Connection:Disconnect()
	return Parts
end

Tool.Activated:Connect(function()
	if debounce then return end
	debounce = true
	
	Tool.Handle.Material = Enum.Material.Neon
	Tool.Handle.BrickColor = BrickColor.new("Institutional white")

	for _,wow in next, GetTouchingParts(hitbox) do
		if wow.Name == "HumanoidRootPart" then
			task.spawn(function()
				print(wow)
				local HRP = wow.Parent:FindFirstChild("HumanoidRootPart")
				local Humanoid = wow.Parent:FindFirstChild("Humanoid") 
				Humanoid.PlatformStand = true
				local BodyVelocity = Instance.new("BodyVelocity", HRP)
				BodyVelocity.Velocity = Vector3.new(math.huge,math.huge,math.huge)
				Tool.Handle.Particle.Enabled = true
				Tool.Handle.Particle2.Enabled = true
				Tool.Handle.lightning_strike:Play()

				local sound = script.Parent.idloveyou:Clone()
				sound.Parent = wow.Parent:FindFirstChild("HumanoidRootPart")
				sound.PlayOnRemove = true
				sound:Destroy()

				game.ReplicatedStorage.Remotes.Camera.SmallExplosion:FireAllClients()

				task.wait(1)
				Tool.Handle.Particle.Enabled = false
				Tool.Handle.Particle2.Enabled = false
				Tool.Handle.Material = Enum.Material.Wood
				Tool.Handle.BrickColor = BrickColor.new("Dirt brown")
				BodyVelocity:Destroy()
				Humanoid.PlatformStand = false
				task.wait(10.479)
				
				Tool.Handle.Material = Enum.Material.Neon
				Tool.Handle.BrickColor = BrickColor.new("Institutional white")
			end)
		end
	end
	
	task.wait(Cooldown)
	debounce = false
end)

Edit: It’s messy but that’s because I haven’t really read through the code you’ve provided. I also suggest breaking the loop if wow.Name == 'HumanoidRootPart. But I am all doing it on assumptions. Let me know if something isn’t how you wanted it.

2 Likes

And if you still want a cooldown on Tool.Activated as well, let me know.

I’ll try this tomorrow, cuz i’m not on my pc :sweat:

Also, yeah i want with the cooldown…
Thanks :slightly_smiling_face:

And to confirm, what the script I provided you does is that when you activate a tool, it checks all the parts it’s hitting only at that moment and then runs your code. Waits until cooldown is over before you can re-activate. Just confirming since looking back at your code, it seems like you may have wanted something like:
Tool activated, checking for Touched objects for 11 seconds. Cooldown ends after those 11 seconds and the .Touched stops checking for those objects.

1 Like

This is the exact reason why you should use magnitude.

:Touched

is not very reliable. It can make it “double-click” or in this example “double touch.”
Now if you use a debounce 99% of the time it won’t “double touch.”
Here is my solution while still using :Touched, :

local Tool = script.Parent.Parent
local hitbox = Tool.Handle.HitBox

local debounce = true

Tool.Activated:Connect(function()
	if debounce == false then
		return
	end
	debounce = false
	Tool.Handle.Material = Enum.Material.Neon
	Tool.Handle.BrickColor = BrickColor.new("Institutional white")
	hitbox.Touched:Connect(function(wow)
		if wow.Name == "HumanoidRootPart" then
			print(wow)
			local HRP = wow.Parent:FindFirstChild("HumanoidRootPart")
			local Humanoid = wow.Parent:FindFirstChild("Humanoid") 
			Humanoid.PlatformStand = true
			local BodyVelocity = Instance.new("BodyVelocity", HRP)
			BodyVelocity.Velocity = Vector3.new(math.huge,math.huge,math.huge)
			Tool.Handle.Particle.Enabled = true
			Tool.Handle.Particle2.Enabled = true
			Tool.Handle.lightning_strike:Play()

			local sound = script.Parent.idloveyou:Clone()
			sound.Parent = wow.Parent:FindFirstChild("HumanoidRootPart")
			sound.PlayOnRemove = true
			sound:Destroy()

			game.ReplicatedStorage.Remotes.Camera.SmallExplosion:FireAllClients()

			wait(1)
			Tool.Handle.Particle.Enabled = false
			Tool.Handle.Particle2.Enabled = false
			Tool.Handle.Material = Enum.Material.Wood
			Tool.Handle.BrickColor = BrickColor.new("Dirt brown")
			BodyVelocity:Destroy()
			Humanoid.PlatformStand = false
			wait(10.479)
			Tool.Handle.Material = Enum.Material.Neon
			Tool.Handle.BrickColor = BrickColor.new("Institutional white")
			debounce = true
		end
	end)
end)
2 Likes

I’ll try both tomorrow! ^^
Thanks for helping me guys :slightly_smiling_face:

1 Like

I’ve used this debounce method for all of my scripts that needed a debounce, and it has always worked.
:slight_smile:

1 Like

So, uh, KLB script worked for me… sometimes the :Touched event still firing multiple times, but not often.

and the script from GodDharma just don’t work, the Handle from the tool turns to Neon and White, but when i touch in some player, just don’t work.

:slightly_smiling_face:

Great! You should also make the debounce wait a little longer if you are having trouble with it again.

Ah. Guess I understood what you wanted incorrectly. Glad you found a solution!