Hello!
I’ve been working on a script who’se purpose is to set parts inbetween the player model and the camera transparent, and when they’re no longer obstructing the character set them back to normal.
The issue is that there is a noticeable split second framedrop whenever my character is obstructed from camera view and the part gets it’s transparency changed.
If you walk with nothing standing inbetween the player model and the camera, there is no lag or split-second drops. Only when I set the objects transparency to 1 or 0.
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService('RunService')
function isEmpty(t)
return next(t) ~= nil
end
RunService.RenderStepped:Connect(function()
local castPoints = {Player.Character.HumanoidRootPart.Position, Player.Character.Head.Position, Player.Character["RightHand"], Player.Character["LeftHand"]}
local IgnoreList = {Player.Character}
for _,i in ipairs(game.Players:GetChildren()) do
table.insert(IgnoreList, i.Character)
end
local arr = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, IgnoreList)
local model = arr
local parts = {}
for _, part in pairs(arr) do
if part.Transparency == 0 then
part.Transparency = 0.75
table.insert(parts, part)
end
end
if(isEmpty(arr)) then
wait(1)
for _, part in pairs(parts) do
part.Transparency = 0
table.remove(parts, _)
end
end
end)
That call to wait(1) in RenderStepped looks weird, and most likely one part of the issue.
Another thing I wondered about, when looking at your code; “what if some parts’ original transparency value is not zero?”
Here is an alternative, where I added comments, to somewhat explain what is the idea / reason:
-- LocalScript
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService('RunService')
-- Dictionary to remember parts that was made transparent
local rememberedParts = {}
-- Variable needed to keep track of when remembered parts is not obscuring anymore
local iterationCount = 0
RunService.RenderStepped:Connect(function(dt)
iterationCount = iterationCount + 1
local castPoints = {
Player.Character.HumanoidRootPart.Position,
Player.Character.Head.Position,
Player.Character["RightHand"],
Player.Character["LeftHand"]
}
local ignoreList = {}
for _, i in ipairs(game.Players:GetChildren()) do
table.insert(ignoreList, i.Character)
end
local arr = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)
-- Loop though all obscuring parts
for _, m in pairs(arr) do
-- Is this a 'not marked before' part,
if rememberedParts[m] == nil then
-- then add it to the rememberedParts, with data containing its original transparency
rememberedParts[m] = { origTransparency = m.Transparency }
-- and make it transparent only once.
m.Transparency = 0.75
end
-- Need to update this 'remembered part', as it was (again) obscuring the player
rememberedParts[m].stillObscuring = iterationCount
end
-- Loop through all the 'remembered parts',
for m, data in pairs(rememberedParts) do
-- if this part was not updated this time around,
if data.stillObscuring < iterationCount then
-- then change its transparency back to normal
m.Transparency = data.origTransparency
-- and remove it from the remembered parts dictionary
rememberedParts[m] = nil
end
end
end)