I’ve noticed that using this script lowers fps considerably when a morph is worn by the player. It is probably due to the amount of checks in the renderstepped but how else could I make it perform the same function but without lagging out the client?
local config = require(game:GetService("ReplicatedStorage").SPH_Assets.GameConfig)
local dead = false
local vehicleSeated
if config.firstPersonBody then
local Character = script.Parent.Parent
local RunService = game:GetService("RunService")
local gunEquipped
local humanoid = Character:WaitForChild("Humanoid")
local torso
if humanoid.RigType == Enum.HumanoidRigType.R6 then
torso = Character:WaitForChild("Torso")
else
torso = Character:WaitForChild("UpperTorso")
end
humanoid.Died:Connect(function()
dead = true
end)
local function CheckForBodyPartNames(model)
for _, part in ipairs(model:GetChildren()) do
local checkPart = Character:FindFirstChild(part.Name)
if checkPart and checkPart:IsA("BasePart") then
if not (string.find(checkPart.Name,"Arm") and gunEquipped) then
return part
end
end
end
end
RunService.RenderStepped:Connect(function(dt)
if not Character:FindFirstChild("Torso") and not Character:FindFirstChild("UpperTorso") or dead or vehicleSeated then
return
elseif Character:FindFirstChild("Head") and Character.Head.LocalTransparencyModifier == 0 then
return
end
-- Check if ACS gun is equipped
gunEquipped = Character:FindFirstChildWhichIsA("Tool")
if gunEquipped and not gunEquipped:FindFirstChild("SPH_Weapon") then gunEquipped = nil end
local armsEnabled = torso["Left Shoulder"].Enabled
-- Loop through character children
for _, obj in ipairs(Character:GetChildren()) do
-- obj is a part
if obj:IsA("BasePart") then
-- Make visible if part is a leg, the torso, or arms when a gun isn't equipped
if (string.find(obj.Name,"Arm") and armsEnabled)
or string.find(obj.Name,"Leg")
or string.find(obj.Name,"Torso")
or string.find(obj.Name,"Foot") then
obj.LocalTransparencyModifier = 0
end
-- obj is a model with parts inside
elseif obj:IsA("Model") and obj:FindFirstChildWhichIsA("BasePart") then
-- Conventional morph
local middlePart = obj:FindFirstChild("Middle") or obj:FindFirstChild("Grip") or CheckForBodyPartNames(obj)
if (middlePart and obj.Name ~= "WeaponRig") then
if string.find(obj.Name,"Holster_") and not config.firstPersonHolsters then continue end
-- Check morph welds
local welds = middlePart:GetJoints()
local badMorph = false
for _, cWeld in ipairs(welds) do
if cWeld.Part0 == Character.Head or cWeld.Part1 == Character.Head then
badMorph = true
break
elseif (string.find(cWeld.Part0.Name,"Arm") or string.find(cWeld.Part1.Name,"Arm")) and gunEquipped then
badMorph = true
break
end
end
if badMorph then continue end
-- Loop through model
for _, part in ipairs(obj:GetDescendants()) do
if part:IsA("BasePart") or part:IsA("Texture") or part:IsA("Decal") then
part.LocalTransparencyModifier = 0
end
end
-- Non conventional morphs that only include one part
elseif #obj:GetChildren() == 1 then
local part = obj:FindFirstChildWhichIsA("BasePart")
local welds = part:GetJoints()
local headMorph = false
for _, cWeld in ipairs(welds) do
-- If welded to the head, ignore
if cWeld.Part0 == Character.Head or cWeld.Part1 == Character.Head then
headMorph = true
break
end
end
if not headMorph then
part.LocalTransparencyModifier = 0
for _, child in ipairs(part:GetChildren()) do
if child:IsA("Texture") or child:IsA("Decal") then
child.LocalTransparencyModifier = 0
end
end
end
end
end
end
end)
humanoid.Seated:Connect(function(seated, seatPart)
if seated and seatPart:IsA("VehicleSeat") then
vehicleSeated = true
else
vehicleSeated = false
end
end)
end```
Instead of calculating the hole process to make the player invisible each frame, make it invisible only one time when needed like so:
--Previous script
local isPlayerVisible = true
local function process ()
local armsEnabled = torso["Left Shoulder"].Enabled
-- Loop through character children
for _, obj in ipairs(Character:GetChildren()) do
-- obj is a part
if obj:IsA("BasePart") then
-- Make visible if part is a leg, the torso, or arms when a gun isn't equipped
if (string.find(obj.Name,"Arm") and armsEnabled)
or string.find(obj.Name,"Leg")
or string.find(obj.Name,"Torso")
or string.find(obj.Name,"Foot") then
obj.LocalTransparencyModifier = 0
end
-- obj is a model with parts inside
elseif obj:IsA("Model") and obj:FindFirstChildWhichIsA("BasePart") then
-- Conventional morph
local middlePart = obj:FindFirstChild("Middle") or obj:FindFirstChild("Grip") or CheckForBodyPartNames(obj)
if (middlePart and obj.Name ~= "WeaponRig") then
if string.find(obj.Name,"Holster_") and not config.firstPersonHolsters then continue end
-- Check morph welds
local welds = middlePart:GetJoints()
local badMorph = false
for _, cWeld in ipairs(welds) do
if cWeld.Part0 == Character.Head or cWeld.Part1 == Character.Head then
badMorph = true
break
elseif (string.find(cWeld.Part0.Name,"Arm") or string.find(cWeld.Part1.Name,"Arm")) and gunEquipped then
badMorph = true
break
end
end
if badMorph then continue end
-- Loop through model
for _, part in ipairs(obj:GetDescendants()) do
if part:IsA("BasePart") or part:IsA("Texture") or part:IsA("Decal") then
part.LocalTransparencyModifier = 0
end
end
-- Non conventional morphs that only include one part
elseif #obj:GetChildren() == 1 then
local part = obj:FindFirstChildWhichIsA("BasePart")
local welds = part:GetJoints()
local headMorph = false
for _, cWeld in ipairs(welds) do
-- If welded to the head, ignore
if cWeld.Part0 == Character.Head or cWeld.Part1 == Character.Head then
headMorph = true
break
end
end
if not headMorph then
part.LocalTransparencyModifier = 0
for _, child in ipairs(part:GetChildren()) do
if child:IsA("Texture") or child:IsA("Decal") then
child.LocalTransparencyModifier = 0
end
end
end
end
end
end
end
RunService.RenderStepped:Connect(function()
if not Character:FindFirstChild("Torso") and not Character:FindFirstChild("UpperTorso") or dead or vehicleSeated then
isPlayerVisible = true
return
elseif Character:FindFirstChild("Head") and Character.Head.LocalTransparencyModifier == 0 then
isPlayerVisible = true
return
end
-- Check if ACS gun is equipped
gunEquipped = Character:FindFirstChildWhichIsA("Tool")
if gunEquipped and not gunEquipped:FindFirstChild("SPH_Weapon") then gunEquipped = nil end
if isPlayerVisible then
isPlayerVisible = false
task.spawn(process)
end
end)
This didn’t work (body is invisible) atleast the way I implemented it didn’t
local dead = false
local vehicleSeated = false
local isPlayerVisible = true
local Character = script.Parent.Parent
local RunService = game:GetService("RunService")
local gunEquipped
local humanoid = Character:WaitForChild("Humanoid")
local torso = Character:WaitForChild("Torso")
humanoid.Died:Connect(function()
dead = true
end)
local function CheckForBodyPartNames(model)
for _, part in ipairs(model:GetChildren()) do
local checkPart = Character:FindFirstChild(part.Name)
if checkPart and checkPart:IsA("BasePart") then
if not (string.find(checkPart.Name, "Arm") and gunEquipped) then
return part
end
end
end
end
local function process()
local armsEnabled = torso["Left Shoulder"].Enabled
-- Loop through character children
for _, obj in ipairs(Character:GetChildren()) do
-- obj is a part
if obj:IsA("BasePart") then
-- Make visible if part is a leg, the torso, or arms when a gun isn't equipped
if (string.find(obj.Name, "Arm") and armsEnabled)
or string.find(obj.Name, "Leg")
or string.find(obj.Name, "Torso")
or string.find(obj.Name, "Foot") then
obj.LocalTransparencyModifier = 0
end
-- obj is a model with parts inside
elseif obj:IsA("Model") and obj:FindFirstChildWhichIsA("BasePart") then
-- Conventional morph
local middlePart = obj:FindFirstChild("Middle") or obj:FindFirstChild("Grip") or CheckForBodyPartNames(obj)
if (middlePart and obj.Name ~= "WeaponRig") then
if string.find(obj.Name, "Holster_") and not config.firstPersonHolsters then continue end
-- Check morph welds
local welds = middlePart:GetJoints()
local badMorph = false
for _, cWeld in ipairs(welds) do
if cWeld.Part0 == Character.Head or cWeld.Part1 == Character.Head then
badMorph = true
break
elseif (string.find(cWeld.Part0.Name, "Arm") or string.find(cWeld.Part1.Name, "Arm")) and gunEquipped then
badMorph = true
break
end
end
if badMorph then continue end
-- Loop through model
for _, part in ipairs(obj:GetDescendants()) do
if part:IsA("BasePart") or part:IsA("Texture") or part:IsA("Decal") then
part.LocalTransparencyModifier = 0
end
end
-- Non conventional morphs that only include one part
elseif #obj:GetChildren() == 1 then
local part = obj:FindFirstChildWhichIsA("BasePart")
local welds = part:GetJoints()
local headMorph = false
for _, cWeld in ipairs(welds) do
-- If welded to the head, ignore
if cWeld.Part0 == Character.Head or cWeld.Part1 == Character.Head then
headMorph = true
break
end
end
if not headMorph then
part.LocalTransparencyModifier = 0
for _, child in ipairs(part:GetChildren()) do
if child:IsA("Texture") or child:IsA("Decal") then
child.LocalTransparencyModifier = 0
end
end
end
end
end
end
end
RunService.RenderStepped:Connect(function()
if not Character:FindFirstChild("Torso") and not Character:FindFirstChild("UpperTorso") or dead or vehicleSeated then
isPlayerVisible = true
return
elseif Character:FindFirstChild("Head") and Character.Head.LocalTransparencyModifier == 0 then
isPlayerVisible = true
return
end
-- Check if ACS gun is equipped
gunEquipped = Character:FindFirstChildWhichIsA("Tool")
if gunEquipped and not gunEquipped:FindFirstChild("SPH_Weapon") then gunEquipped = nil end
if isPlayerVisible then
isPlayerVisible = false
task.spawn(process)
end
end)
humanoid.Seated:Connect(function(seated, seatPart)
if seated and seatPart:IsA("VehicleSeat") then
vehicleSeated = true
else
vehicleSeated = false
end
end)```