How would i add GetPartsObscuringTarget using ViewportFrames

Hello, I want to make a minimap using ViewFrames but the issue is parts that are in the way, i found out I can use Camera:GetPartsObscuringTarget but im not sure how to.

Here is my code:

local viewPort = script.Parent:WaitForChild("ViewportFrame")
local charPart = viewPort:WaitForChild("Player")

local roads = workspace:WaitForChild("Roads")
local buildings = workspace:WaitForChild("Buildings")

local hotkey = Enum.KeyCode.M
local UIS = game:GetService("UserInputService")
local open = false
local frame = script.Parent.Parent

UIS.InputBegan:Connect(function(key, gp)
	
 if key.KeyCode == hotkey then
  if UIS:GetFocusedTextBox() == nil then
	
   if open == false then
    open = true
	frame.Visible = open
	elseif open == true then
    open = false
    frame.Visible = open
   end
  end
 end
end)

roads:Clone().Parent = viewPort
buildings:Clone().Parent = viewPort

local camera = Instance.new("Camera", workspace)
camera.CameraType = "Scriptable"

local height = 300
local player = game.Players.LocalPlayer
repeat wait() until player.Character

viewPort.CurrentCamera = camera

game:GetService("RunService").RenderStepped:Connect(function ()
	local position1 = player.Character.HumanoidRootPart.Position
	camera.CFrame = CFrame.new(Vector3.new(position1.X,height,position1.Z), position1)
	charPart.CFrame = player.Character.HumanoidRootPart.CFrame
end)

--raycasting
local roads = workspace:WaitForChild("Roads")
local buildings = workspace:WaitForChild("Buildings")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = character:FindFirstChild("Head")
local ray = Ray.new(head.Position, Vector3.new(0,50,0))
local Camera = game.Workspace.Camera

while wait(1) do
	local ray = Ray.new(head.Position, Vector3.new(0,50,0))
	local whiteList = {roads, buildings} 
	local hit, position = game.Workspace:FindPartOnRayWithWhitelist(ray, whiteList)
	if hit then
		print('ray hit a part!')
	else
		print('no part found')
	end
end

I get a print in the output saying that my ray has hit a part but i want that part to be removed, can someone please help me?

I wouldn’t recommend using ViewportFrames to make a minimap since they can use up lots of resources, instead of using a ViewportFrame, I’d recommend using a mini map image unless you’re attempting to create a 3d map.

If you’d like to make a map using an image, here’s a module you may find useful
Minimap module - Minimap creator

If you would rather use ViewportFrames, here’s how you could use GetPartsObscuringTargets to find if a part is Obscuring the camera.

The method GetPartsObscuringTargets takes 2 arguments according to the object browser in studio GetPartsObscuringTarget(Array castPoints, Objects ignoreList), it takes an array of cast points and an array of objects which will be ignored, in your case, you should set the cast point to the players head or the players HumanoidRootPart since all descendants of the object character will be ignored (in the ViewportFrame) and the ignore list should consist of objects or parts which shouldn’t be returned in the table returned by the function.

Here’s some code you could use as a reference

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

while true do
  local Parts = Camera:GetPartsObscuringTarget({HumanoidRootPart.Position}, {Character})
  for i, v in pairs(Parts) do
    print(i)
    print(v)
  end
  wait()
end