How to ignore mobs colliding on top of each other with raycast

The code works perfectly fine but when mobs start going in each other the raycast params just seem to ignore it which is kind of annoying since I want to hit all mobs regardless if theyre inside each other.

for _, MobInstance in CollectionService:GetTagged("Mob") do
			local Torso = MobInstance:FindFirstChild("Torso")
				and MobInstance.Torso
			
			if not Torso then
				return
			end

			local Raycast = RaycastParams.new()
			Raycast.FilterType = Enum.RaycastFilterType.Exclude
			Raycast.FilterDescendantsInstances = {Character, MobInstance}
			
			local Resultant = Torso.Position - CharacterTorso.Position
			local Normal_Resultant = Resultant.Unit
			local Distance = Resultant.Magnitude

			local Dot = Look:Dot(Normal_Resultant)
			local Rad_Angle = math.acos(Dot)
			local Deg_Angle = math.deg(Rad_Angle)

			local In_Range = Distance <= Range
			local In_View = Deg_Angle <= Angle
			local Is_Hit = In_Range and In_View

			local True_Resultant = workspace:Raycast(CharacterTorso.Position,Resultant,Raycast)

			if not True_Resultant and Is_Hit then
				task.spawn(function()
					RequestStunMob(MobInstance, StunValue)
					
					Knockback:Activate(Torso, KnockbackValue, Position, Torso.Position)
					
					if Damageable then
						Damage:DamageMob(Player, MobList[MobInstance], {Ignore = DamageIgnore, Damage = DamageValue})
					end

					pcall(function()
						Torso:SetNetworkOwner(Player)
					end)
				end)
			end
		end
1 Like

Try this

for _, MobInstance in CollectionService:GetTagged("Mob") do
    local Torso = MobInstance:FindFirstChild("Torso")
    if not Torso then
        return
    end

    local Resultant = Torso.Position - CharacterTorso.Position
    local Normal_Resultant = Resultant.Unit
    local Distance = Resultant.Magnitude

    local In_Range = Distance <= Range
    local In_View = Look:Dot(Normal_Resultant) >= math.cos(math.rad(Angle))
    local Is_Hit = In_Range and In_View

    if Is_Hit then
        local True_Resultant = workspace:Raycast(CharacterTorso.Position, Resultant, Raycast)

        if not True_Resultant or True_Resultant.Instance == MobInstance then
            task.spawn(function()
                RequestStunMob(MobInstance, StunValue)
                Knockback:Activate(Torso, KnockbackValue, Position, Torso.Position)

                if Damageable then
                    Damage:DamageMob(Player, MobList[MobInstance], {Ignore = DamageIgnore, Damage = DamageValue})
                end

                pcall(function()
                    Torso:SetNetworkOwner(Player)
                end)
            end)
        end
    end
end

Nope it still doesnt work for some reason, still does the same thing as the old script nothing changed

The problem is probably as follows:

This excludes a single mob. That means every other mob is still included, which would make it so that not all mobs can be hit in case of collision.

You’re better off excluding every other MobInstance, so that it can only get that 1 mob (but also terrain etc).

local mobs = CollectionService:GetTagged("Mob")
table.remove(mobs,table.find(mobs,MobInstance)
Raycast.FilterDescendantsInstances = {Character, table.unpack(mobs)}