Adding new parts to ignore while casting ray

In order to increase performance for my game, I want to only show tiles within a set distance from the player.
The problem is when the ray does hit its neighbouring tile, the tile doesn’t seem to be added to its
FilterDescendantsInstances, with only the first neighbouring tiles being shown.

function LoadTilesModule:LoadTiles()
	for _, Direction in pairs(self.DirectionTable) do
		for i=5, 0, -1 do	-- Show all tiles within 5 tiles of the player
			local RayResult = workspace:Raycast(self.HexOrigin, Direction, self.RayParams)
			print(self.RayParams.FilterDescendantsInstances)
			if RayResult then
				local Result = RayResult.Instance
				if Result.Parent.Name == "Tile" then
					table.insert(self.RayParams.FilterDescendantsInstances, Result)  -- Add new parts to ignore
					ShowTile(Result)					
				end
			end
			
		end
	end
end

self.DirectionTable is a table consisting of vector3’s which are used to cast a ray in different directions.

Only the tile the player is standing on is being filtered out in print(self.RayParams.FilterDescendantsInstances)
image

When setting the FilterDescendantsInstances property of RaycastParams, everything is copied and then made immutable, so the table contained in the property cannot be changed, only overwritten. This can be solved by adding the object to a separate table and passing that table into the property again.

It should also be mentioned that getting the table from the RaycastParams does work, so you could try changing your “Result” block to something like this:

if Result.Parent.Name == "Tile" then
	local FilteredDescendants = self.RayParams.FilterDescendantsInstances
	table.insert(FilteredDescendants, Result)  -- Add new parts to ignore
	self.RayParams.FilterDescendantsInstances = FilteredDescendants
	ShowTile(Result)					
end

The table assigned to the property cannot be changed dynamically, for example by adding new children/descendants to any of the instances contained inside of the table (just an extension of what you stated).