Hitbox being destroyed at the wrong time

Hey there, I am using this for my combat system hitbox : Raycast Hitbox 4.01: For all your melee needs!

The issue I am having is that if I have one function that start’s and stop’s the hitbox. Well sometimes it’s gonna stop the hitbox on the second time it’s called do to the task.wait.

Here is a quick video example and the source code. I recall that it happens when spamming the call of the function. Basically what should happen is that if the function is called again it should stop everything that happened in the previous one. Watch 2024-05-06 13-31-59 | Streamable

Source code

local CombatHitbox = {}
local RS = game:GetService("ReplicatedStorage")
local Modules = RS:WaitForChild("Modules")
local RaycastingHitbox = require(Modules:WaitForChild("OpenSource"):WaitForChild("RaycastHitboxV4"))
local CombatData = require(Modules:WaitForChild("Shared"):WaitForChild("CombatData"))
local ServerStorage = game:GetService("ServerStorage")
local ServerModules = ServerStorage:WaitForChild("ServerModules")
local CombatModules = ServerModules:WaitForChild("CombatModules")
local CombatStunManager = require(CombatModules:WaitForChild("CombatStunManager"))

function CombatHitbox.Hitbox(player, M1Animation, weaponName, animationName)
	local Char = player.Character
	local WeaponHitbox = Char:WaitForChild(weaponName)
	local Hitbox = RaycastingHitbox.new(WeaponHitbox.Hitbox)
	local weaponData = CombatData.GetWeapon(weaponName)
	Hitbox:HitStart()
	print("start")
	task.spawn(function()
		CombatStunManager.ApplyStun(player.Character, 0.3, "IsPunching")
	end)
	
	Hitbox.OnHit:Connect(function(hit, humanoid)
		if humanoid and humanoid.Parent and humanoid.Parent ~= player.Character then
			if humanoid.Parent:GetAttribute("IsParrying") then

			else
				local damageAmount = animationName and weaponData.Damages[animationName] or 10
				humanoid:TakeDamage(damageAmount)
				CombatStunManager.ApplyStun(humanoid.Parent, 0.5, "IsStunned")
			end
		end
	end)
	task.wait(weaponData.HitboxDurations[animationName])
	Hitbox:HitStop()
	print("stop")
end

return CombatHitbox

Make sure that each time the CombatHitbox.Hitbox() function is called, it properly handles the termination of any ongoing hitbox operations before starting a new one. One way is by keeping track of the active hitbox instance and stopping it before starting a new one.