Trouble with melee hitting multiple targets

Hi. I’ve been having trouble with this melee hitting multiple targets.

local Hits = {}		
		DamagePart.Touched:Connect(function(hit)
			local HitHumanoid = hit.Parent:FindFirstChild("Humanoid")
			if HitHumanoid and CanDamage == true then
				if hit.Parent:FindFirstChild("TEAM").Value ~= Character:FindFirstChild("TEAM").Value then
					--------------------------------------------------------
					local found
					for _, character in next, Hits do
						if character == HitHumanoid then
							found = true
						end
					end
					
					if not found then
						if not table.find(Hits, HitHumanoid) then
							table.insert(Hits, HitHumanoid)
							Hits[HitHumanoid] = false
						end
						if hit.Parent:FindFirstChild("Attacker") then
							hit.Parent:FindFirstChild("Attacker").Value = "" .. Player.Name
						end
					end
					--------------------------------------------------------
					if Hits[HitHumanoid] == false then
						Event:FireServer("MeleeHit", hit, HitHumanoid, hit.Parent:FindFirstChild("HumanoidRootPart"))
						Hits[HitHumanoid] = true
					end
					--------------------------------------------------------
					delay(.5, function()
						for i, character in next, Hits do
							if character == HitHumanoid then
								table.remove(Hits, i)
							end
						end
					end)
					--------------------------------------------------------
				end
			end
		end)

This is the main script that focuses around hitting the enemy. It DOES in fact hit multiple targets, but it doesn’t have a debounce for each individual target (i assume), so it is able to hit the same target like 300 times before the hitwindow is closed. Any help is appreciated.

Instead of using “touched,” utilize raycasting instead. Essentially, attach attachments to your sword or any other object where the blade is located. Then, project the raycast at an offset of the attachment towards the direction of the swing or any desired direction. This approach enables the detection of blocked swings, hitting terrain, and accurately pinpointing the point of impact.

Here is a snippet of the code you need:

for _, attachment in ipairs(attachments) do
				if attachment:IsA('Attachment') then
					local lastPosition = attachment.WorldPosition
					local startTime = tick()
					spawn(function()
						while tick() - startTime <= duration do
							local currentPosition = attachment.WorldPosition
							local raycastParams = RaycastParams.new()
							raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
							raycastParams.FilterDescendantsInstances = {tool, char}

							local direction = (currentPosition - lastPosition).Unit * 1 -- extend the raycast as far as possible

							local raycastResult = workspace:Raycast(lastPosition, direction, raycastParams)

							-- If the raycast didn't hit anything, we'll use the end of the raycast as the position
							local hitPosition = raycastResult and raycastResult.Position or (lastPosition + direction)

							local trailPart = Instance.new("Part")
							trailPart.Anchored = true
							trailPart.CanCollide = false
							trailPart.Size = Vector3.new(.1, .1, (hitPosition - lastPosition).Magnitude)
							trailPart.CFrame = CFrame.lookAt(lastPosition, hitPosition) * CFrame.new(0, 0, -trailPart.Size.Z / 2)
							trailPart.Parent = workspace
							trailPart.Color = Color3.fromRGB(255, 0, 4)
							if raycastResult then
								if raycastResult.Instance then
									if raycastResult.Instance.Parent then
										if raycastResult.Instance.Parent:FindFirstChild('Humanoid') then
											if osTouched < os.time() - .5 then
												osTouched = os.time()
												raycastResult.Instance.Parent.Humanoid:TakeDamage(33)

											end
										end
									end
								end
							end
										

							Debris:AddItem(trailPart, 0.5)

							lastPosition = currentPosition
							RunService.Heartbeat:Wait()
						end
					end)
					
				end
			end

Here I actually didn’t offset the raycast backwards for more accurate detection there is a ton of thigs to improve

If you’re looking for a literal box (or another static shape), I’d suggest using “worldroot:GetPartBoundsInBox()” and looping it for how many times you like for the duration of the hitbox.

But if you want something that accurately potrays the arc of the swing, I’d suggest using Raycast Hitbox,