Camera Obscuring Help

I am trying to make a system for my custom camera where if there is a part in between your player and the camera, the part’s transparency turns half transparent. My ObscureCamera function is fired in a RenderStepped function.

local function ObscureCamera(Camera,Character)
	-- transparency effects
	Objects = Camera:GetPartsObscuringTarget({Character.HumanoidRootPart.Position},{})
		
	-- add new obscuring parts to table
	for _,obscuringPart in pairs(Objects) do -- for ever obscuring part do
		local Object = GetModelParent(obscuringPart) or obscuringPart
		if IsInTable(ObscuringPartsData,Object,false) == false then -- if the part is not already obscuring
			if not IsClass(obscuringPart.Parent,{"Accessory"}) then -- if part isnt part of a filtered class

				table.insert(ObscuringPartsData,Object)
				
				if Object:IsA("Model") then
					print("ISAMODEL")
					for _,child in pairs(Object:GetDescendants()) do
						if child:IsA("BasePart") then
							child.LocalTransparencyModifier = 0.5
						end
					end
				else
					Object.LocalTransparencyModifier = 0.5
				end
			end
		end
	end
		
	-- remove changed obscuring parts
	for index,obscuringObject in pairs(ObscuringPartsData) do
		local Object = GetModelParent(obscuringObject) or obscuringObject
		if IsInTable(Objects,Object,false) == false then
			
			if Object:IsA("Model") then
				table.remove(ObscuringPartsData,index)
				for _,child in pairs(Object:GetDescendants()) do
					if child:IsA("BasePart") then
						child.LocalTransparencyModifier = 0
					end
				end
			else
				table.remove(ObscuringPartsData,index)
				Object.LocalTransparencyModifier = 0
			end
		end
	end
end

So what I am trying to do is if the part is in a model then get the model and change all the children to half transparent. I can correctly get the main parent model with this function:

local function GetModelParent(Object)
	if Object.Parent == workspace or Object.Parent:IsA("Folder") then
		if Object.ClassName ~= "Model" then return nil end
		--print(Object.Name)
		return Object
	else
		return GetModelParent(Object.Parent)
	end
end

My method works for if it’s just a part, but not for models. I will show both ways below:

Only a Part, no model
https://gyazo.com/abaccaa3b1d0ab37eaee7c973c50437e

Model Objects
https://gyazo.com/fe17766866adb9cba50568a75ce5bb11

For some reason, with models it is removing and I proved this by printing on the remove bit.

How can I make this work correctly?

6 Likes

It appears to me that the flashing happens because the LocalTransparencyModifier is being set to 0.5 and then back to 0.
I suspect that the if statements that decide which parts become transparent and which parts stay opaque are both true for models.
I’d recommend double-checking the if statements. Make sure that the one that sets transparency to 0.5 and the one that sets to 0 are never both true for the same object, as that’ll cause flickering.

2 Likes

I triple checked and said my methods out loud to make sense of them and idk why they still aren’t working correctly.

2 Likes

Try attaching a print() to debug before the lines that change transparency. Make them print out the part’s name, the model’s name, and what the transparency is about to be set to. That way, you can see what lines are running incorrectly.

I solved it by adding a

if IsInTable(Objects,Object,false) then return end

in the remove bit of my method.

5 Likes

Awesome, glad to hear that worked.

3 Likes

Is this a local script or a script and where does it go?