There's a better way of going about this; however, this will require chaning a bit of your code. :o
Firstly, instead of using multiple variables, we can use a For loop, which can be setup as:
local AHolderVariable = {}
local BHolderVariable = {}
Now, you may be wondering "where's the loop?" But we need Tables for the For loop(s).
Instead of repeatedly creating the same code over-and-over again, let's use a function instead:
function AddToList(list, key)
for i, v in pairs(list) do
if key == 'A' then
if v.Name == 'a' .. i .. 'holder' then
table.insert(AHolderVariable, v)
end
elseif key == 'B' then
if v.Name == 'b' .. i .. 'holder' then
table.insert(BHolderVariable, v)
end
end
end
end
AddToList(game:GetService('ReplicatedStorage'):GetChildren(), 'A')
Now, for removing the assets, there's a way rather than having to create multiple if statements to remove them from your game, even so, 'remove' is deprecated, and is recommended not to use.
To remove the assets, we can, again, use the For loop to accomplish this, and it'll reduce the amount of code used; however, it will still use a bit of code, but hopefully not as much as before:
function CheckOrRemoveAssets(list)
for i, v in pairs(list) do
if v.Parent == game:GetService('Workspace') then
v:Destroy()
end
end
end
CheckOrRemoveAssets(AHolderVariable)
After accomplishing all this, we now have the final product, but before we're done, I'd like to explain the problem w/ your original code:
1. You put your variables inside the function, which isn't necessarily bad, but in your case it was a big mistake; here's how it is working: Get the assets from ReplicatedStorage while checking @ the same time afterwards; however, to fix this, you can put the variables outside of the function. :)
2. You used a lot of unnecessary code, such as using a lot of If statements when you could've used a For loop to do that, and decrease the amount of code used; your way of doing it wasn't necessarily bad, but there's another way. :)
3. This is a bit tricky, especially if you're starting out, but you could use a For loop to add the assets into a table, then let another For loop check that table, such as I've in the code I used here; again, it may be tricky, but for me, it's a lot more efficient, and better. :)
And Wa-la! We have shortened your code by so much, while making it more efficient for use!
Note: I have not personally tested any of this code; I typed it w/o going into the studio, so it may be buggy, but if it is breaking or is too buggy, please contact me immediately. :)
Other code touched on, but may needed to be explained further: Destroy, GetChildren, If statements
I hope this helped, as it's been a while since I've been on! :D
3 comments
Honestly, this feels like one of the worst answers I've ever given. XD
Hey you're back! The last I checked you were gone for around two months or something.
@wfvj014 Ya. :P The reason is because I was tired of my questions not getting answered, so I went to the ROBLOX forums to ask questions there for a while, and it was pretty fun. :)