Raycast filter does not work?

I tried to make npc AI that detects players in sight and track, but raycast does not work properly,
as it prints parts that’s included in raycast filter.

ray.FilterType = Enum.RaycastFilterType.Blacklist

		local rayFilter = {}
		for _, part in pairs(script.Parent:GetDescendants()) do
			if part:IsA("BasePart") then
				table.insert(rayFilter, part)
			end
		end
		for _, part in pairs(workspace:GetDescendants()) do
			if part:IsA("BasePart") then
				if part.Transparency > 0.5 then
					table.insert(rayFilter, part)
				end
			end
		end
		ray.FilterDescendantsInstances = rayFilter
		
		print(ray.FilterDescendantsInstances)
		
		local raycastLine = math.sqrt(stat.raycastCount.Value)
		local raycastDistance = stat.raycastDistance.Value
		local headCF = head.CFrame
		for countV = -raycastLine, raycastLine do
			for countH = -raycastLine, raycastLine do
				local result = workspace:Raycast(headCF.Position, (headCF * CFrame.Angles(0, math.rad(countH), math.rad(countV))).LookVector * raycastDistance)
				if result then
					print(result.Instance:GetFullName())
					for _, player in pairs(game:GetService("Players"):GetChildren()) do
						pcall(function()
							if player.Character then
								if result.Instance:IsDescendantOf(player.Character) then
									if not table.find(target, player.Character) then
										table.insert(targets, player.Character)
										status = "tracking"
										print("found entity")
									end
								end
							end
						end)
					end
				end
			end
		end

output:
{ --this is raycast filter descendants instances
~
Workspace.Sniper.BFG 50.Laser2Node
~
}

–this is raycast.Instance:GetFullName() output
Workspace.Sniper.BFG 50.Laser2Node

it printed Laser2Node even Laser2Node is included in filter descendants instances.

1 Like

What is script.Parent searching? It’s probably better to condense the for loop, or instead use folders and place your parts in their, and blacklist the folders instead.

Sniper is script.Parent (script is ScriptAI)
캡처

is Sniper.BFG 50.Laser2Node a basepart?

yes it is
캡처

I found problem, problem was
local result = workspace:Raycast(headCF.Position, (headCF * CFrame.Angles(0, math.rad(countH), math.rad(countV))).LookVector * raycastDistance)

because there was no raycast param after direction