Problem with bullet system

Hi! I am making the bullet wallbanging, which if you shoot a bullet to the wall, it has a chance to pierce though the wall and keep flying. I found a problem while I am in the really end of the project.

The system works really well when there is no humanoid on the bullet direction, so there is no need discussing this case

The problem started when I added a Humanoid as the specimen. If the Humanoid stand in front of me, the system still works.

However, if I place the Humanoid behind a wall, the system works fine with the “Head” but goes crazy with the other part of the Humanoid

Output (when shooting at any BodyPart except the “Head”):

The code

local x = math.random(weaponData.spread*-100, weaponData.spread*100)/100
			local y = math.random(weaponData.spread*-100, weaponData.spread*100)/100
			local origin = camera.CFrame.p
			local position = mouse.Hit.p
			local direction = (, position) * CFrame.Angles(math.rad(x), math.rad(y), 0)).LookVector
			local raycastParams =
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			raycastParams.IgnoreWater = true
			local part, part2, part3
			local distance, distance2, distance3
			local wallbangDistance = 0
			local distancePenetrated = 0
			local damage = 0
			local decreasedDamage = 0
			local ignoredPart
			--First raycast, find first part hit
			raycastParams.FilterDescendantsInstances = {camera, character, ignore}
			part = workspace:Raycast(origin, direction.Unit * 5000, raycastParams)
			distance = (origin - part.Position).Magnitude
			damage = weaponData.damage - distance * .02
			hitscan(part.Instance, part.Position, damage)
			direction = part.Position - origin
			ignoredPart = part.Instance
			--Loop for wallbang raycasting
			while distancePenetrated < weaponData.piercingDepth do
				--2nd raycast
				raycastParams.FilterDescendantsInstances = {camera, character, ignore, ignoredPart}
				part2 = workspace:Raycast(origin, direction.Unit * 5000, raycastParams)
				if part2 == nil then
					distance2 = (origin - part2.Position).Magnitude

					--3rd raycast
					direction = part.Position - part2.Position
					raycastParams.FilterDescendantsInstances = {camera, character, ignore}
					part3 = workspace:Raycast(part2.Position, direction.Unit * 5000, raycastParams)
					if part3 == nil then
						distance3 = (part2.Position - part3.Position).Magnitude
						print("Distance1:", distance)
						print("Distance2:", distance2)
						print("Distance3:", distance3)
						wallbangDistance = distance2 - distance3 - distance
						distancePenetrated += materialCheck(part, wallbangDistance)
						if distancePenetrated < weaponData.piercingDepth then
							hitscan(part3.Instance, part3.Position, 0)
							decreasedDamage = materialCheck(part, wallbangDistance) * 2
							damage = damage - decreasedDamage - distance3
							hitscan(part2.Instance, part2.Position, damage)
							origin = part3.Position
							position = part2.Position
							direction =, position).LookVector
							distance = distance3

							ignoredPart = part2.Instance

I don’t know what I have done wrong in this code. Any suggestion is appreciated.

try putting a task.wait at the while true do

It will make the script runs infinitely, since there is no stop

[Self-solved] I used “ignoredPart” to determine which part will be ignored in 2nd raycast, and Humanoid parts dont love it. Seems like I have to find another way