Raycast doesn't exclude part

I’m trying to exclude parts but the ray detects them anyway.

local function hasLineOfSight(pos, player)
	local char = player.Character
	if not char or not char.PrimaryPart then return false end
	local rootPos = char.Head.Position

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

	-- ALSO exclude tagged "Window" parts
	for _, window in ipairs(CollectionService:GetTagged("Window")) do
		table.insert(rayParams.FilterDescendantsInstances, window)
	end

	local ray = workspace:Raycast(pos, (rootPos - pos), rayParams)
	print(ray)
	return ray == nil -- no hit means clear line of sight
end
local function hasLineOfSight(pos, player)
	local char = player.Character
	if not char or not char.PrimaryPart then return false end
	local rootPos = char.Head.Position

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

	-- ALSO exclude tagged "Window" parts
	for _, window in ipairs(CollectionService:GetTagged("Window")) do
		rayParams:AddToFilter(window)
	end

	local ray = workspace:Raycast(pos, (rootPos - pos), rayParams)
	print(ray)
	return ray == nil -- no hit means clear line of sight
end

could you try this?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.