Why doesn't the GetPartsObscuringTarget example code work?

Hello DevForums! I wanted to learn how to use this in my own game, so I decided to use the example code that comes with the article to test it out.

Code:

local function XRay(castPoints, ignoreList)
	ignoreList = ignoreList or {}
	local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)
 
	for _, part in pairs(parts) do
		part.LocalTransparencyModifier = 0.75
		for _, child in pairs(part:GetChildren()) do
			if child:IsA("Decal") or child:IsA("Texture") then
				child.LocalTransparencyModifier = 0.75
			end
		end
	end
end

I put this in a localscript which is parented to the player gui, but it doesn’t work. I tried putting it in the starterpack also, but it still doesn’t work. I was wondering if anyone had an idea to why it doesn’t work.

So what exactly is this supposed to do?

Suggesting by the article, it will get any Baseparts obscuring the camera and change its transparency modifier to make it transparent.

Could I get a video of what is happening? I’m just curious why this is not working. Any errors?

Try putting it in starter character scripts or starter player scripts.

I tried both and it still wasn’t working. It was still in localscripts btw.

Edit: I also tried putting them there while in just normal scripts, but it still wasn’t working.

Are you ever calling

With the castPoints

There were no errors. As the article says,

This function will make any BaseParts , Decals or Textures coming between the Workspace.CurrentCamera and the given cast points translucent.

It was supposed to make parts translucent, but instead, it just zooms in and does not make the block transparent.

Do you mean calling like:

local castPoints = {Vector3.new(0, 10, 0), Vector3.new(0, 15, 0)}

because it seems like this called the cast point:

local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)

Well it should be:

local castPoints = {CameraPosition, PositionFarAwayOnTheDirectionOfWhereTheFunctionShouldLookForParts (WhatTheCameraIsLookingAt)}

Should the “What the camera is looking at” be the path to the part the camera should be focused on like the humanoid root part? Also, the camera position should just be a new vector3 , right? I am particularly confused on the “PositionFarAwayOnTheDirectionOfWhereTheFunctionShouldLookForParts” meant.

I phrased this badly, this is the thing the camera is looking at.

Both should be the position (vector3)

It should be:

game.Workspace.CurrentCamera.CFrame.Position

So would this work as a script?

local function XRay(castPoints, ignoreList)
	local castPoints = {game.Workspace.CurrentCamera.CFrame.Position, game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")}
	ignoreList = ignoreList or {}
	local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)

	for _, part in pairs(parts) do
		part.LocalTransparencyModifier = 0.75
		for _, child in pairs(part:GetChildren()) do
			if child:IsA("Decal") or child:IsA("Texture") then
				child.LocalTransparencyModifier = 0.75
			end
		end
	end
end

Edit: To be clear, where would this go? The starterpack or the startergui?

You made a slight mistake:

local function XRay(castPoints, ignoreList)
	local castPoints = {game.Workspace.CurrentCamera.CFrame.Position, game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position} -- Added .Position
	ignoreList = ignoreList or {}
	local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)

	for _, part in pairs(parts) do
		part.LocalTransparencyModifier = 0.75
		for _, child in pairs(part:GetChildren()) do
			if child:IsA("Decal") or child:IsA("Texture") then
				child.LocalTransparencyModifier = 0.75
			end
		end
	end
end

Would there be any other mistakes? I put this fix in, but it still doesn’t seem to work.

Do something after the script piece like:

game:GetService('RunService').Stepped:Connect(function()
XRay()
end)

Thank you, this works! I realized that I had to call it in someway.

1 Like