Simplification of first person limbs display

Hi folks!
I have this code that makes character body parts, and some of my modified body parts, visible while in first person. Though this code works, it causes a lot of lag for user who has additional body parts, since code loops through all the parts that custom limb consists of and turns off transparency on every frame. I’m looking for advice how I can simplify this, so that it causes minimum to no lag.
Note: player has forced locked first person mode on spawn, but when player dies, he is forced in third person without being able to go first person to spectate others.
Also yes, this code is not mine, I found it on tutorial and modified myself.

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

local FPMaximumDistance = 0.6 -- For scalability, but keep it at 0.6 since it is the right distance.
local FirstPersonLocalTransparency = 0
local ThirdPresonLocalTransparency = 0

local function SetCharacterLocalTransparency(transparency)
	-- looping thru player children to find limbs
	if Character:FindFirstChild("Left Arm") then
		Character:FindFirstChild("Left Arm").LocalTransparencyModifier = transparency
	end
	if Character:FindFirstChild("Right Arm") then
		Character:FindFirstChild("Right Arm").LocalTransparencyModifier = transparency
	end
	Character:FindFirstChild("Left Leg").LocalTransparencyModifier = transparency
	Character:FindFirstChild("Right Leg").LocalTransparencyModifier = transparency
	
	if Character:FindFirstChild("Tail") then
		for _, tailPart in pairs(Character:FindFirstChild("Tail"):GetDescendants()) do
			if tailPart:IsA("Part") or tailPart:IsA("CornerWedgePart") then
				tailPart.LocalTransparencyModifier = transparency
			end
		end
	end
	
	if Character:FindFirstChild("Left Arm") then
		if Character:FindFirstChild("Left Arm"):FindFirstChild("MechanicalPartL") then
			for _, armPart in pairs(Character:FindFirstChild("Left Arm"):FindFirstChild("MechanicalPartL"):GetDescendants()) do
				if armPart:IsA("Part") or armPart:IsA("MeshPart") or armPart:IsA("UnionOperation") then
					armPart.LocalTransparencyModifier = transparency
				end
			end
		end
	end
	
	if Character:FindFirstChild("Right Arm") then
		if Character:FindFirstChild("Right Arm"):FindFirstChild("MechanicalPartR") then
			for _, armPart in pairs(Character:FindFirstChild("Right Arm"):FindFirstChild("MechanicalPartR"):GetDescendants()) do
				if armPart:IsA("Part") or armPart:IsA("MeshPart") or armPart:IsA("UnionOperation") then
					armPart.LocalTransparencyModifier = transparency
				end
			end
		end
	end
end

RunService.RenderStepped:Connect(function()
	local isfirstperson = (Head.CFrame.Position - Camera.CFrame.Position).Magnitude < FPMaximumDistance -- Determine wether we are in first person
	if (isfirstperson) then
		SetCharacterLocalTransparency(FirstPersonLocalTransparency)
	else
		SetCharacterLocalTransparency(ThirdPresonLocalTransparency)
	end
end)

i’d make it more modular; it might prevent potential future issues. also, no guarentees that this will work; but use HumanoidRootPart:GetPropertyChangedSignal("CFrame"), So it only listens to when the player is moving, and not every frame rendered.

if need help with modules a really simple function goes like:

return function(transparency)
     -- Write whatever logic you have here
end

thanks for reply, but I actually managed to solve it by changing the way function is called (and by deleting some useless lines)

local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = game.Workspace.CurrentCamera
local Head = Character:WaitForChild("Head")
local RENDER_PRIORITY = Enum.RenderPriority.Character.Value - 5



local function SetCharacterLocalTransparency()
	-- looping thru player children to find limbs
	if Character:FindFirstChild("Left Arm") then
		Character:FindFirstChild("Left Arm").LocalTransparencyModifier = 0
	end
	if Character:FindFirstChild("Right Arm") then
		Character:FindFirstChild("Right Arm").LocalTransparencyModifier = 0
	end
	Character:FindFirstChild("Left Leg").LocalTransparencyModifier = 0
	Character:FindFirstChild("Right Leg").LocalTransparencyModifier = 0
	
	if Character:FindFirstChild("Tail") then
		for _, tailPart in pairs(Character:FindFirstChild("Tail"):GetDescendants()) do
			if tailPart:IsA("Part") or tailPart:IsA("CornerWedgePart") then
				tailPart.LocalTransparencyModifier = 0
			end
		end
	end
	
	if Character:FindFirstChild("Left Arm") then
		if Character:FindFirstChild("Left Arm"):FindFirstChild("MechanicalPartL") then
			for _, armPart in pairs(Character:FindFirstChild("Left Arm"):FindFirstChild("MechanicalPartL"):GetDescendants()) do
				if armPart:IsA("Part") or armPart:IsA("MeshPart") or armPart:IsA("UnionOperation") then
					armPart.LocalTransparencyModifier = 0
				end
			end
		end
	end
	
	if Character:FindFirstChild("Right Arm") then
		if Character:FindFirstChild("Right Arm"):FindFirstChild("MechanicalPartR") then
			for _, armPart in pairs(Character:FindFirstChild("Right Arm"):FindFirstChild("MechanicalPartR"):GetDescendants()) do
				if armPart:IsA("Part") or armPart:IsA("MeshPart") or armPart:IsA("UnionOperation") then
					armPart.LocalTransparencyModifier = 0
				end
			end
		end
	end
end

RunService:BindToRenderStep("CharacterTransparency", RENDER_PRIORITY, SetCharacterLocalTransparency)

Currently it causes nearly no lag with those changes

1 Like

awesome, mark your reply as solved so we can close le topic

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.