GetPartsInPart being unreliable

Hello! I am trying to make a simple sword using GetPartsInPart but for some reason its not working properly.

Heres the code that checks for collision:

local curHitbox
		local curHitboxDamage = 0
		local curHitboxPriority = 0
		local function Damage(Enemy)
			local OverParam = OverlapParams.new()
			OverParam.FilterDescendantsInstances = {Enemy:GetChildren()}
			OverParam.FilterType = Enum.RaycastFilterType.Include
			for i,hitbox in pairs(script.Parent.Hitboxes:GetChildren()) do
				for i,part in pairs(workspace:GetPartsInPart(hitbox,OverParam)) do
					if hitbox:GetAttribute("Damage") > curHitboxDamage or  hitbox:GetAttribute("Priority") > curHitboxPriority and (part.Position - hitbox.Position).Magnitude < 1 then
						curHitbox = hitbox
						curHitboxDamage = hitbox:GetAttribute("Damage")
					end
				end
			end
			if curHitbox and DamageDeb == true then
				game.ReplicatedStorage.MeleeDamageRequest:FireServer(Enemy,curHitbox,Enemy.HumanoidRootPart)
				tool.Handle.HitHumanoid:Play()
				curHitbox.BrickColor = BrickColor.new(1,0,0)
				DamageDeb = false
				task.wait(0.5)
				curHitbox.BrickColor = BrickColor.new(1,1,1)
			end
		end
		for i,player in pairs(game.Players:GetChildren()) do
			if player ~= game.Players.LocalPlayer then
				Damage(player.Character)
			end
		end
		for i,bot in pairs(workspace.Enemies:GetChildren()) do
			Damage(bot)
		end
	end

Heres the bug happening shown in a video:


Help would be greatly appreciated!

2 Likes

Try using an official hitbox module or raycast the striking mechanism; both options are reliable but I believe it’d be more performance effective and reliable to simply cast a ray in front of the user.