RayCastParams excluding parts it shouldn't be

Hi,

I have code which sends out many rays from the origin in a sort of square formation. I am trying to filter out any transparent parts which is use this funciton for:

local workspaceParts = workspace:GetDescendants()
local transparentParts = {}

for i, part in ipairs(workspaceParts) do
	if part.ClassName == "Part" then
		if part.Transparency > 0 then
			table.insert(transparentParts, part)
		end
	end
end

If I print this table it is fine. However when I create the rays it only produces one line of rays when I try to exclude the transparent parts like this:

local createdParts = {}

for y = -10, 10, 1 do -- Vertical offset
	for x = -10, 10, 1 do -- Horizontal offset
		
		
		-- Calculate the direction
		local offsetDirection = lighting:GetSunDirection() + Vector3.new(x, y, 0)

		-- Cast a ray from the origin in the direction
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {Origin, unpack(createdParts), unpack(transparentParts)}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		local raycastResult = workspace:Raycast(Origin.Position, offsetDirection - Origin.Position, raycastParams)

		-- Check if the ray hits something
		if raycastResult then
			-- Create the part at the hit position
			local part = Instance.new("Part")
			part.Name = "RayPart"
			part.Size = Vector3.new(1, 1, 1)
			part.Position = raycastResult.Position
			part.Material = Enum.Material.Neon
			part.BrickColor = BrickColor.new("New Yeller")
			part.Parent = workspace
			part.Anchored = true

			table.insert(createdParts, part)

			local rayPart = drawline(Origin.Position, part.Position)

			table.insert(createdParts, rayPart)
		end
	end
end

This code runs and it looks like this:

However if I leave out the exclusion of transparent parts it looks like this:

raycastParams.FilterDescendantsInstances = {Origin, unpack(createdParts)}

The code should be going through the window when I exclude the transparent parts. As you can see the window is only a small section of the building.

Thanks in advance for any help :+1:

The issue is the table construction for the filter. A table literal (or anything that can accept multiple values at once, for that matter) will only receive all of the values of a multi-var if nothing else comes after it, otherwise all items past the first will be lost. In the line…

raycastParams.FilterDescendantsInstances = {Origin, unpack(createdParts), unpack(transparentParts)}

…the second unpack() truncates the return values of the first, and so only the first object in the createdParts array will actually be included in the new table. As a result, the parts created for visualization purposes end up blocking raycasts because they aren’t a part of the filter.

To avoid this, you can create a dedicated filter table, add objects to that table as they are created, and then reassign raycastParams.FilterDescendantsInstances each time, but ROBLOX has recently added the RaycastParams:AddToFilter() method to simplify the process.

Try this:

-- Declare "raycastParams" up-scope so that it can be reused for all raycasts.
-- The initial filter can be set to the "Origin" part and any transparent parts.
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Origin, unpack(transparentParts)}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

for y = -10, 10, 1 do -- Vertical offset
	for x = -10, 10, 1 do -- Horizontal offset

		-- Calculate the direction
		local offsetDirection = lighting:GetSunDirection() + Vector3.new(x, y, 0)

		-- Cast a ray from the origin in the direction		
		local raycastResult = workspace:Raycast(Origin.Position, offsetDirection - Origin.Position, raycastParams)

		-- Check if the ray hits something
		if raycastResult then
			-- Create the part at the hit position
			local part = Instance.new("Part")
			part.Name = "RayPart"
			part.Size = Vector3.new(1, 1, 1)
			part.Position = raycastResult.Position
			part.Material = Enum.Material.Neon
			part.BrickColor = BrickColor.new("New Yeller")
			part.Parent = workspace
			part.Anchored = true

			table.insert(createdParts, part)
			raycastParams:AddToFilter(part)	-- Add this visualization part to the filter.

			local rayPart = drawline(Origin.Position, part.Position)

			table.insert(createdParts, rayPart)
			raycastParams:AddToFilter(rayPart)	-- Also add this visualization part to the filter.
		end
	end
end
1 Like

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