How can I improve my hit detection system?

So I already have a working hit detection system, the problem is that if a player only partially hides on a wall, the player can’t take damage as long as his HumanoidRootPart is not exposed from the enemy’s perspective (So, if the player hides with only the arm exposed, the player can’t take damage, as long as the player doesn’t fully expose itself). Is there a way I can improve this?


HitEvent.OnServerEvent:Connect(function(player : Player, victim : Model, damage : number, Equip : string, MousePos : Vector3, Barrel : Part)
	
	if not HitTable[victim.Name .. "" .. player.Name] then
		HitTable[victim.Name .. "" .. player.Name] = {}
	end

	local GunConfig = GunTable[Equip].GunConfig

	local rayfilter = RaycastParams.new()
	rayfilter.FilterDescendantsInstances = {player.Character}
	rayfilter.FilterType = Enum.RaycastFilterType.Exclude

	local humanoid = victim:FindFirstChildOfClass("Humanoid")
	local prevhealth = humanoid.Health
	
	-- Hit detection starts here
	
	local direction = (victim.PrimaryPart.Position - Barrel).Unit
	local maxDistance = 6000

	local primaryRay = workspace:Raycast(Barrel, direction * maxDistance, rayfilter)

	if primaryRay then
		local hit = primaryRay.Instance
		local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")

		if hitHumanoid and hit.Parent == victim then
			if player.Character:GetAttribute("DamageBoost") then
				damage += 10
			end
			if GunConfig.Ammo.AmmoType == "Slag" then
				EffectHandlerNew:ApplyEffect(victim, "Burning")
			end
			player.GunFolder.Hits.Value += 1
			hitHumanoid:TakeDamage(damage)
			DCE:FireClient(player, damage) -- Damage Counter Event
		else
			CancelHit:FireClient(player, victim.Humanoid, victim.Humanoid.Health)
		end

	else
		CancelHit:FireClient(player, victim.Humanoid, victim.Humanoid.Health)
	end
	
	--...
	
end)

Conserve the RaycastParams so it isnt constantly being created then discarded whenever a RemoteEvent is called.
When its time to check if they were hit by something, update the filter to include the player info.