so basically i’m generating a cache of assets to preload by using this script:
local cache = {} for _, v in pairs(workspace:GetDescendants) do if v:IsA( mesh or decal ) then insert the decal/mesh texture/meshid into the table end end
you get the point.
Problem is, there’s a lot of “” (blanks) and duplicate strings. How can I filter these out?
-- fixed your script
local cache = {}
for _, v in pairs(workspace:GetDescendents()) do
if v:IsA("Mesh") or v:IsA("Decal") then
table.insert(cache, v)
end
end
The table shouldn’t be inserting duplicate strings or nil
s as workspace:GetDescendants
returns an array with objects. Unless you’re indexing their name.
You could run this function however:
local function gc(t)
local clean = {}
for i, o in ipairs(t) do
if not (o == nil or (tostring(o):gsub("%s+", "") == "") or o == "") then
table.insert(clean, o)
end
end
return clean
end
cache = gc(cache)
For the record if you’re passing this into PreloadAsync then there’s two things that you should know:
-
PreloadAsync will automatically pass in the descendants of an object. Passing in the Workspace will pass Workspace as well as its descendants.
-
This isn’t proper use of PreloadAsync. The only things you should be filtering through PreloadAsync are assets the player needs to see immediately.
(a) Preloading everything is the same as preloading nothing and if you’re doing this solely for a trackable loading screen then it becomes a UX problem of long loading screens. IIRC there was a technical problem with this too and it was discouraged by an engineer.
@HugeCoolboy2007 Wouldn’t recommend manipulating a table during an iteration like that. table.remove will shift the indices down and you’ll end up skipping some iterations. There’s been a few problem threads on the forum about using table.remove in ipairs.
Hmm, ok. But still, in other cases, do you know how to filter duplicates and blanks from a table.
Run through your table with an i,v in pairs loop.
Check if
table.length
is equal to zero.
If so,
table.remove(table, v)
Alright, that solves the blanks, now about the duplicates
In my case, not really. I never really have a need to handle these kinds of cases and if I do I fix the cause of the duplicates or blanks rather than the code. Frankly I don’t know the best way to handle this.
Is the index/ordering of the tables important to you? If it isn’t, you can iterate over the table the same way HugeCoolboy suggested but with pairs instead of ipairs and by directly removing the index out of the table instead of using table.remove. This will leave some gaps so you’ll have to treat your table as a dictionary rather than a contiguous array (e.g. numeric for loops and ipairs will no longer work for it).
Duplicates I can’t help you with because you’re filling your table with instance references and you’d have to be checking if one instance is equal to another for n amount of instances which is absurd, but blank strings should be relatively easy to handle.
local function cleanUnwanted(tbl)
for key, value in pairs(tbl) do
if typeof(value) == "string" and not value:match("%S+") then
tbl[key] = nil
end
end
end
Sometimes there are blank asset ids (“”) or duplicate strings “rbxassetid://thing”, “rbxassetid://samething”
so I use this
local blanksanddupe = {
"";
"Test";
"Test";
}
table.foreach(blanksanddupe, function(i, v)
if not v:match("%S+") then
table.remove(blanksanddupe, i)
end
end)
print(blanksanddupe[1])
And it prints out “Test” as expected. Thanks!