Hitbox Duration and deboucne duration increase more and more every use

so today i made this fireaxe melee for one of my side projects, it was working fine until i noticed that throughout every use of it, the hitbox duration and the debounce duration increased more and more to the point it lasted almost forever, if anyone knows why this happens and could give me a possible fix, i would highly appreciate it

Code (ServerSided because i dont trust the client)
Tool.Activated:Connect(function()
	if CanAttack == true and DB == false then
		DB = true
		AttackAnim:Play()
		if AnimatedWM:FindFirstChild("Trail") then
			AnimatedWM:WaitForChild("Swing"):Play()
			AnimatedWM:WaitForChild("Trail", 1).Enabled = true
		else
		end
		local Damage = coroutine.wrap(function()			
			RUNSERVICE.Heartbeat:Connect(function()
				wait()
				for i,Model in pairs(game.Workspace:GetChildren()) do			
					if Model:IsA("Model") and Model:FindFirstChild("HumanoidRootPart") and DB == true and Model ~= Character and Swinged == false then
						if (AnimatedWM.Position - Model.HumanoidRootPart.Position).Magnitude <= 4.4 then
							local SoundsTable = {}
							for _, v in pairs(AnimatedWM:WaitForChild("Sounds"):GetChildren()) do
								table.insert(SoundsTable, v)
							end
							local RandomSound = SoundsTable[math.random(1, #SoundsTable)]					
							RandomSound:Play()
							Swinged = true
							Model:WaitForChild("Humanoid"):TakeDamage(18)
						else
						end
					else
					end
				end
			end)
		end)
		wait(0.4)
		Damage()
			wait(0.3)
			if AnimatedWM:FindFirstChild("Trail") then
				AnimatedWM:WaitForChild("Trail", 1).Enabled = false
			else
			end
			Swinged = true
			wait(1.4)
			Swinged = false			
			DB = false
	end
end)

Oh boy. It looks like the service is connecting too many events for every time Tool.Activated is fired and it won’t stop.

its also affecting the equip animation and idle for some reason now, is that related?

Uhh, probably? I think the server is taking a beating after several activation. Try equipping and idling without activating it and compare with the state after using it.

If that’s not the case, I think you’re handling the animations the wrong way or the code is too expensive to handle all that at once. The worst offender is game.Workspace:GetChildren().

1 Like

i’ll try replacing it with another type of loop and see if that helps

huh it actually worked, i suppose i’ll never abuse runservice ever again, thanks for the help though, even though i’ve been scripting for 3 years stuff like this still fly over my head, once again thanks for the help

1 Like