Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Touched event still fires repeatedly even with a debounce. How can I fix it?

I was trying to make a punch tool, but I encountered a problem where the touched event fires repeatedly even WITH a debounce. I used a LocalScript with a RemoteEvent. There are also no errors.

Local Script: (I am having trouble with lines around 32 and 64.)

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContentProvider = game:GetService("ContentProvider")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local tool = script.Parent
local configurationFolder = script.Parent:WaitForChild("Configuration")

local debounce = false

tool.Activated:Connect(function()
	if not debounce then
		debounce = true
		
		local humanoid = localPlayer.Character:WaitForChild("Humanoid")
		
		if humanoid.RigType == Enum.HumanoidRigType.R6 then
		
			local animation = Instance.new("Animation")
			animation.AnimationId = "rbxassetid://04875004180"
			animation.Parent = humanoid
			
			local animationToLoad = {animation}
			ContentProvider:PreloadAsync(animationToLoad)
			
			local loadedAnimation = humanoid:LoadAnimation(animation)
			
			loadedAnimation:Play()
			
			local hitDebounce = false
			
			localPlayer.Character:WaitForChild("Right Arm").Touched:Connect(function(hit)
				if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
					if hit.Parent:WaitForChild("Humanoid").Health > 0 and not hitDebounce then
						hitDebounce = true
						
						local targetCharacter = hit.Parent
					
						ReplicatedStorage:WaitForChild("KillHumanoidEvent"):FireServer(targetCharacter:WaitForChild("Humanoid"), script.Parent:WaitForChild("punch"), configurationFolder:WaitForChild("Damage").Value)

						wait(1)
						hitDebounce = false
						
						return nil
					end
				end
			end)
			
		else
			
			local animation = Instance.new("Animation")
			animation.AnimationId = "rbxassetid://4875026093" 
			animation.Parent = humanoid
			
			local animationToLoad = {animation}
			ContentProvider:PreloadAsync(animationToLoad)
			
			local loadedAnimation = humanoid:LoadAnimation(animation)
			
			loadedAnimation:Play()
			
			local hitDebounce = false
			
			localPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
				if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
					if hit.Parent:WaitForChild("Humanoid").Health > 0 and not hitDebounce then
						hitDebounce = true
							
						local targetCharacter = hit.Parent
								
						ReplicatedStorage:WaitForChild("KillHumanoidEvent"):FireServer(targetCharacter:WaitForChild("Humanoid"), script.Parent:WaitForChild("punch"), configurationFolder:WaitForChild("Damage").Value)
	
						wait(1)
						hitDebounce = false
							
						return nil
					end
				end
			end)
		end
		
		wait(1)
		debounce = false
	end
end)

Server Script: (It works fine.)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContentProvider = game:GetService("ContentProvider")

ReplicatedStorage:WaitForChild("KillHumanoidEvent").OnServerEvent:Connect(function(firerPlayer, targetHumanoid, sound, damage)
	local soundToLoad = {sound}
	ContentProvider:PreloadAsync(soundToLoad)
	
	targetHumanoid:TakeDamage(damage)
	sound:Play()
end)

6 comments

0

Try printing lets see what it doss.

0

If that doesnt work retype it by scratch.

0

The first time I punch it, it prints once. The second time I punch it, it prints twice.

0

At line 2 do Players.PlayerAdded:Connect(function(player) end)

0

Okay, but how would that help because LocalScript's run on the client, so the script wouldn't run without a player. Plus, LocalScript's only run on a tool when equipped. (As it's a descendant of a player's character.)

0

I tried it and now it doesn't even work.

1 answer

✓ Accepted answer

Nothing seems wrong with your code however you the only assumption would be that you must put a disconnect function when the player stops punching or it will continue to run in no matter what you do.

1 comment

0

Yes, I figured that out, as I said in the site chat.

This discussion is preserved as it was. New answers, comments, and votes are closed.