There are definitely some ways to clean up this code.
First, half of your code is all the way on the right side of the screen! Nesting can make code look really gross. To fix this, we can use what are called guard clauses.
I’ve refactored the original here to use guard clauses rather than conditional nesting:
function pendDestroy(NameToClean)
local notificationContainer = baseClip:FindFirstChild('Container')
for index, descendant in next, (baseClip:FindFirstChild('Container'):GetChildren()) do
if descendant.Name ~= "Notif Clone" then continue end -- This is a guard clause!
if SupportCalls[NameToClean] == nil or {} then continue end -- Also, I've seen you use "nil or {}" after the == sign here. I didn't realize you could do that. Cool!
local Succ,Msg = pcall(function()
for a,b in pairs(SupportCalls[NameToClean]) do
if b ~= descendant then continue end
for c,d in pairs(Stacks.Notifs) do
if d ~= b then continue end
table.remove(Stacks.Notifs,c)
end
b:Destroy()
table.remove(SupportCalls[NameToClean],a)
figureNotifs(Stacks.Notifs,notificationContainer)
end
end)
end
end
Secondly, we can simplify that function you’re pcalling. To me — and correct me if I’m wrong — it looks like you’re trying to remove the descendant from SupportCalls[NameToClean] and Stacks.Notifs. We can use table.find to get the index of the descendant without having to loop through them. I’ve made these changes here:
function pendDestroy(NameToClean)
local notificationContainer = baseClip:FindFirstChild('Container')
for index, descendant in next, (baseClip:FindFirstChild('Container'):GetChildren()) do
if descendant.Name ~= "Notif Clone" then continue end
if SupportCalls[NameToClean] == nil or {} then continue end
local Succ,Msg = pcall(function()
-- I've changed quite a bit here. It might not fit in exactly with what you're trying to do, so you might have to change it. The point is that there's a more direct way to remove an object from a table other than iterating through it.
local position
position = table.find(SupportCalls[NameToClean], descendant)
if position then
table.remove(SupportCalls[NameToClean], descendant)
end
position = table.find(Stacks.Notifs, descendant)
if position then
table.remove(Stacks.Notifs], descendant)
end
descendant:Destroy()
figureNotifs(Stacks.Notifs,notificationContainer)
end)
end
end
You’ll then notice some repeated operations for removing from the tables, which we can factor out:
local function removeFromTable(t, value) -- using t here because table is already reserved
position = table.find(t, descendant)
if not position then return end
table.remove(t, position)
end
function pendDestroy(NameToClean)
local notificationContainer = baseClip:FindFirstChild('Container')
for index, descendant in next, (baseClip:FindFirstChild('Container'):GetChildren()) do
if descendant.Name ~= "Notif Clone" then continue end
if SupportCalls[NameToClean] == nil or {} then continue end
local Succ,Msg = pcall(function()
removeFromTable(SupportCalls[NameToClean], descendant)
removeFromTable(Stacks.Notifs, descendant)
descendant:Destroy()
figureNotifs(Stacks.Notifs,notificationContainer)
end)
end
end
Next, I honestly think we can remove the pcall. The table removing function doesn’t risk erroring. If figureNotifs risks erroring, then we can pcall that individually.
That brings us to our FINAL RESULT:
local function removeFromTable(t, value)
position = table.find(t, descendant)
if not position then return end
table.remove(t, position)
end
function pendDestroy(NameToClean)
local notificationContainer = baseClip:FindFirstChild('Container')
for index, descendant in next, (baseClip:FindFirstChild('Container'):GetChildren()) do
if descendant.Name ~= "Notif Clone" then continue end
if SupportCalls[NameToClean] == nil or {} then continue end
removeFromTable(SupportCalls[NameToClean], descendant)
removeFromTable(Stacks.Notifs, descendant)
descendant:Destroy()
figureNotifs(Stacks.Notifs,notificationContainer)
end
end
I may have made some mistakes, so if it doesn’t work, you might have to fiddle around with it. If you know your game well, you should be able to modify it to make it work for you. These are just the changes that I would make.