IM trying to achieve a better optimized replication of the code, currently it iterates through cached descendants before changing their colors through lerping constantly.
My main issues regarding how quickly it iterates is the performance drops constantly. My game is focused on visuals, mainly color swapping. Overall, I have not been able to find any resources on helping its performance and just need help or tips on how I can make the script less laggy. (144fps drops to around 60-50, this is horrible)
-- this is the cached function which saves objects into a table, then returns the objects used in color changing:
local function CacheDescendants(target, Name)
local cachedTrails = {}
local cachedParticles = {}
local cachedBillboards = {}
local cachedNeonParts = {}
local cachedPointLights = {}
for _, desc in pairs(target:GetDescendants()) do
local isBlack = nil
--Check if the descendant is black based on its type
if desc:IsA("BasePart") then
isBlack = desc.Name == "Ignore"
elseif desc:IsA("ParticleEmitter") then
isBlack = desc.Color.Keypoints[1].Value == Color3.new(0, 0, 0)
elseif desc:IsA("Trail") then
isBlack = desc.Color.Keypoints[1].Value == Color3.new(0, 0, 0)
elseif desc:IsA("Beam") then
isBlack = desc.Color.Keypoints[1].Value == Color3.new(0, 0, 0)
end
-- Only add to tables if not black or named ignored?
if not isBlack then
if desc:IsA("Trail") then
table.insert(cachedTrails, desc)
elseif desc:IsA("ParticleEmitter") or desc:IsA("Beam") then
table.insert(cachedParticles, desc)
elseif desc:IsA("BillboardGui") then
table.insert(cachedBillboards, desc)
elseif (Name ~= "Desolation" and Name ~= "Nihilism") and (desc:IsA("BasePart") and desc.Material == Enum.Material.Neon or desc:IsA("PointLight")) then
table.insert(cachedNeonParts, desc)
elseif desc:IsA("PointLight") then
table.insert(cachedPointLights, desc)
end
end
end
return cachedTrails, cachedParticles, cachedBillboards, cachedNeonParts, cachedPointLights
end
--the connection for color creation/handling:
Local updateConnection
updateConnection = runService.Heartbeat:Connect(function(dt)
--target is what holds those color changing objects,particles, etc
local cachedTrails, cachedParticles, cachedBillboards, cachedNeonParts,cachedPointLights = CacheDescendants(target, Name)
--this funciton handles how the colors are arranged/changed constantly
local function UpdateColorsAndParticles(newSpeed, minSpeed, maxSpeed, sensitivity, Color31, Color32, overrideColor, FlashColor, flashCounter)
-- Pre-calculate values
local speedRatio = math.clamp((newSpeed - minSpeed) / (maxSpeed - minSpeed), 0, 1)
local colorLerp = math.abs(math.sin(currentTick * sensitivity))
local loudness = music.PlaybackLoudness
-- Calculate interpolated color
local color = Color3.new(
(Color32.R - Color31.R) * colorLerp + Color31.R,
(Color32.G - Color31.G) * colorLerp + Color31.G,
(Color32.B - Color31.B) * colorLerp + Color31.B
)
-- Apply override or flash colors
if overrideColor then
color = overrideColor
elseif FlashColor and flashCounter > 0 then
color = FlashColor
flashCounter = flashCounter - 1
end
-- ColorSequence to avoid redundant creation
local colorSequence = ColorSequence.new(color)
-- Update objects in ao loop
local function updateObject(obj)
if obj:IsA("ParticleEmitter") or obj:IsA("Trail") or obj:IsA("Beam") then
if obj.Color ~= colorSequence then
obj.Color = colorSequence
end
if obj:IsA("ParticleEmitter") and obj:FindFirstChild("PulseOnBeat") then
local pulseValue = loudness / (obj:FindFirstChild("PulseOnBeat").Value or 100)
obj.Size = NumberSequence.new(pulseValue)
end
elseif obj:IsA("BillboardGui") then
local textLabel = obj:FindFirstChildWhichIsA("TextLabel")
if textLabel and textLabel.TextColor3 ~= color then
textLabel.TextColor3 = color
end
elseif obj:IsA("BasePart") then
if obj.Color ~= color then
obj.Color = color
end
elseif obj:IsA("PointLight") then
if obj.Color ~= color then
obj.Color = color
end
end
end
-- Loop through all objectss
for _, obj in ipairs(cachedTrails) do updateObject(obj) end
for _, obj in ipairs(cachedParticles) do updateObject(obj) end
for _, obj in ipairs(cachedBillboards) do updateObject(obj) end
for _, obj in ipairs(cachedNeonParts) do updateObject(obj) end
for _, obj in ipairs(cachedPointLights) do updateObject(obj) end
end
UpdateColorsAndParticles(newSpeed, minSpeed, maxSpeed, sensitivity, Color31, Color32, overrideColor, FlashColor, flashCounter)
end)
Overall, I’m starting to think it might be impossible to optimize or do…