How to fix the Players function error: UserID.Player Scripts.Player Module.Camera Module.TransparencyController:195: In camera mode, Scriptable

Hello everyone, I’m making a script for a first-person camera and using a Scriptable camera since I myself prescribed the movement in the script that I need, but there is a problem that I can’t figure out how to remove it.

My function is for transparency of body parts and accessories:

local function updateTransparencyOnce()
	if v1.dead then
		for _, part in pairs(v1.char:GetDescendants()) do
			if part:IsA("BasePart") then
				part.LocalTransparencyModifier = 0
			end
		end
		else
			for _, part in pairs(v1.char:GetDescendants()) do
				if part:IsA("BasePart") then
					part.LocalTransparencyModifier = 1
	
				if not (
					part.Parent ~= v1.char and 
						not part:FindFirstAncestorOfClass("Tool") and 
						(not part:FindFirstAncestorOfClass("Model") or not part:FindFirstAncestorOfClass("Model"):GetAttribute("ShowInFirstPerson")) and 
						not part:FindFirstAncestor("OffhandHandle") and 
						part.Name ~= "OffhandHandle"
					) or (
							part:FindFirstChildOfClass("WrapLayer") and 
							part:FindFirstChild("FaceFrontAttachment") == nil and 
							part:FindFirstChild("FaceCenterAttachment") == nil and 
							part:FindFirstChild("HairAttachment") == nil and 
							part:FindFirstChild("HatAttachment") == nil and 
							part:FindFirstChild("NeckRigAttachment") == nil and 
							part.Parent:GetAttribute("HideInFirstPerson") ~= true
					) then
					if not (part.Name ~= "Torso") or part.Name == "Head" then
						part.LocalTransparencyModifier = 1
					else
						part.LocalTransparencyModifier = 0
					end
				end
			end
		end
	end
end

updateTransparencyOnce()

It works, but it’s in roblox player, not in the studio, that it causes a bunch of errors related to the main roblox script, which is responsible for the character…

don’t forget that I use camera.CameraType = Enum.CameraType.Scriptable because there may be an error due to this, but I do not know how to fix it in this mode. I have tried many methods but I have not been able to find a solution.

Alright, let me see…

Sometimes player’s body parts (humanoidrootpart, Humanoid, Head, etc.) haven’t loaded before code checks and tries to use those. If something makes an error then the code won’t work. I also have similar problems and that’s why I always put

if not [variable] then return end

I also recommend adding as many if statements as possible. Also you can add print(“smth”) that happens when if statement is false. This way you can see what causes an error.

I tried to add these and little bit modified the code:

local function updateTransparencyOnce()
	if not v1 or not v1.char then return end -- Prevent errors if v1 or character doesn't exist.

	if v1.dead then
		for _, part in pairs(v1.char:GetDescendants()) do
			if part:IsA("BasePart") then
				part.LocalTransparencyModifier = 0
			end
		end
	else
		for _, part in pairs(v1.char:GetDescendants()) do
			if part:IsA("BasePart") then
				-- Default transparency
				part.LocalTransparencyModifier = 1

				-- Conditions for exceptions
				local parent = part.Parent
				local hasWrapLayer = part:FindFirstChildOfClass("WrapLayer") ~= nil
				local noAttachments =
					part:FindFirstChild("FaceFrontAttachment") == nil and
					part:FindFirstChild("FaceCenterAttachment") == nil and
					part:FindFirstChild("HairAttachment") == nil and
					part:FindFirstChild("HatAttachment") == nil and
					part:FindFirstChild("NeckRigAttachment") == nil

				local showInFirstPerson = parent and parent:GetAttribute("ShowInFirstPerson") == true
				local hideInFirstPerson = parent and parent:GetAttribute("HideInFirstPerson") == true

				-- Check conditions
				if not (parent ~= v1.char and not part:FindFirstAncestorOfClass("Tool") and not showInFirstPerson and not part:FindFirstAncestor("OffhandHandle") and part.Name ~= "OffhandHandle") or (hasWrapLayer and noAttachments and not hideInFirstPerson) then
					if part.Name == "Torso" or part.Name == "Head" then
						part.LocalTransparencyModifier = 1
					else
						part.LocalTransparencyModifier = 0
					end
				end
			end
		end
	end
end

updateTransparencyOnce()

The last if statement is a bit long :3. You can change and make it look a bit cleaner. Hope this helps.

1 Like