Damage event fires multiple timesw

I’m making a combat system for my friend, and I’ve ran into a problem with the Damage Event firing multiple times.

LocalScript
local function CanDamage()
		local blade = Tool.Blade
		
		local touchingParts = GetTouchingParts(blade)
		for i, v in pairs(touchingParts) do
			if table.find(damageParts, v.Name) and not table.find(ignoreParts, v.Name) and v.Parent ~= character then
				print(tostring(v).." :candamage function")
				return v
			end
		end
	end

AttackRightShieldlessAnim.KeyframeReached:Connect(function(keyframeName)
		if keyframeName == "CanHit" then
			local part = CanDamage()
			print(tostring(part).." :anim function")
			
			if part then
				local attackType = animation.AttackType.Value
				local attackLength = animation.Length.Value

				DamageEvent:FireServer(part.Parent, Tool, attackType)
			end
		end
	end)
end

So in my LocalScript, when a specific keyframe of an animation is reached, it checks if it can damage by looping through the parts, and then returns a part that can be damaged. Then the script checks if there is a returned part, and then, it fires an event to server to damage.

ServerScript
DamageEvent.OnServerEvent:Connect(function(eventPlayer, character, Tool, attackType)
	print("server sided script recieved damage event")
	local Values = Tool.Values
	local SlashDamage = Values.SlashDamage.Value
	local PierceDamage = Values.PierceDamage.Value
	local BludgeDamage = Values.BludgeDamage.Value

	local attackdebounce = false

	local weaponNames = WeaponsSettings:GetChildren().Name

	local enemyWeapon = checkForWeapon(character)
	local enemyHum = character.Humanoid


	if attackType == "Slash" then
		enemyHum:TakeDamage(SlashDamage)
	end
end)

And in total, if I press leftMouseButton twice or more, aka activate the tool multiple times, then it attacks the same amount of times you clicked, or even more.

EDIT: Also the CanDamage() function always returns nil even on a dummy for some reason

Do you have an event responsible for damage dealing inside of the tool’s Activated function? Cause looking at what you said, that should be what’s causing multiple damage fires, though we don’t have a lot of code to know if that’s the case

And for the CanDamage, can we see what GetTouchingParts is? Could be related to that or the conditions in the loop preventing it, can you print touchingParts to see if it’s an issue with the function or the condition

1 Like

Could the function be looping the return part in CanDamage()? If so, wouldn’t if part then be run multiple times each time part is returned? Sorry not sure exactly how running functions with loops in a variable works

1 Like

full script

LocalScript
Tool.Activated:Connect(function()
		if debounce == false then
			if #playingAnimations == 0 then
				debounce = true
				AttackRightShieldlessAnim:AdjustSpeed(2)
				AttackRightShieldlessAnim:Play()
			end
		end

		AttackRightShieldlessAnim.Stopped:Connect(function()
			debounce = false
		end)

		local damageParts = {"Head"; "Left Arm"; "Left Leg"; "Right Arm"; "Right Leg"; "Torso"; "HumanoidRootPart"}
		local ignoreParts = {"Handle"; "ToolPart"}

		local function GetTouchingParts(part)
			print(tostring(part).." :getparts function")
			local connection = part.Touched:Connect(function() end)
			local results = part:GetTouchingParts()
			connection:Disconnect()
			return results
		end

		local function CanDamage()
			local blade = Tool.Blade

			local touchingParts = blade:GetTouchingParts(blade)
			for i, v in pairs(touchingParts) do
				if table.find(damageParts, v.Name) and not table.find(ignoreParts, v.Name) and v.Parent ~= character then
					print(tostring(v).." :candamage function")
					return v
				end
			end
		end

		AttackRightShieldlessAnim.KeyframeReached:Connect(function(keyframeName)
			if keyframeName == "CanHit" then
				local part = CanDamage()
				print(tostring(part).." :anim function")

				if part then
					local attackType = AttackRightShieldlessAnim.AttackType.Value
					local attackLength = AttackRightShieldlessAnim.Length.Value

					print("firing an event")
					DamageEvent:FireServer(part.Parent, Tool, attackType)
				end
			end
		end)
end)

i dont think so
most of the time i’ve done return in a for i, v loop it stopped the whole loop returning only 1 variable

Have you tried possibly adding a debounce?

1 Like

There’s your issue with it firing multiple times, you put the events in the Activated Event, meaning it’ll create the KeyframeReached event multiple times

And for the CanDamage thing, try using a method from one of the new Spatial queries since the creator of that method you’re using for GetTouchingParts recommends it

1 Like

theres only an event in replicated storage and its fired in the script

yeah i did

30 hcarsssss

1 Like

so i should create a separate function outside of the Tool.Activated event? i dont really understand the problem

1 Like

From the looks of it, your AttackRightShieldlessAnim isn’t initalized in the Activated event, so just put the KeyframeReached event outside of the Activated event, and other events too.

Though if you want it so that the damaging only happens when you activate the tool, use some sort of variable that detects when you’re attacking, in this case, check if the debounce is false

2 Likes

Your AttackRightShieldlessAnim.KeyframeReached event is the one @EmbatTheHybrid is talking about. Every time Tool.Activated is triggered, AttackRightShieldlessAnim.KeyframeReached:Connect(function() ... will create a new connection.

You’ve probably confused RBXScriptConnections (or simply Connections) and RBXScriptSignals (or called Signals) from RemoteEvents.

Move this code:

outside of the Tool.Activated function.

2 Likes