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)