i want to check if theres decals inside an object, but i want to do it without doing something like this:
for i,decals in pairs(part:GetChildren()) do
if decals:IsA("Decal") then
-- blah blah blah
end
end
since my code is spaghetti enough and i want to make it more optimized, and mainly to reduce lag since its in a for loop that does its thing every renderstepped
You should be able to use an event such as .ChildAdded to make it run less times.
Otherwise if you are adding a decal in another script you should be able to :Fire a signal to let other scripts know to do this stuff.
yes but the thing is the decal isn’t being added, i probably should make myself more clear with what i’m trying to do
i’m making a chromatic aberration script, and it clones every part in workspace every renderstepped using a for loop (i know that causes lag, i’ll optimize it later) and i want to check if theres a texture/decal in the part that it’s cloning to remove it, causing less lag and also making the chromatic aberration fit the style my game is in more
but using a for loop would be pretty meh to do, since it both makes my script more messy and could cause more lag, not completely sure about that though
but if there isn’t any other way then it’s alright i guess
It would be inefficient to constantly generate a new table of children and traverse it every frame like what you described, so instead you should be utilizing events to create a singular table for all your decals.
local Children: {Instance} = script:GetChildren()
local Decals: {Decal} = table.create(#Children) -- Pre-Allocation
do
local function OnChildAdded(Child: Instance): ()
if Child:IsA("Decal") then
table.insert(Decals, Child)
end
end
local function OnChildRemoved(Child: Instance): ()
if Child:IsA("Decal") then
local Index: number? = table.find(Decals, Child)
if Index then
table.remove(Decals, Index)
end
end
end
for Index: number, Child: Instance in Children do
OnChildAdded(Child)
end
script.ChildAdded:Connect(OnChildAdded)
script.ChildRemoved:Connect(OnChildRemoved)
end
Then all you have to do is traverse the Decals array each frame. Hooray for optimization!