Help with tables

I was just making basic Asset preloader, but I found this dumb error where it loads only the first Service in the table, then it ignores the others. Any way I can change anything?

local assets = {game:GetService("StarterGui"),game:GetService("ReplicatedStorage"),game:GetService("Workspace")}

local needtobeloaded = {}
for Id,Asset in pairs(table.unpack(assets):GetDescendants()) do
	if Asset:IsA("MeshPart") or Asset:IsA("BlockMesh") or Asset:IsA("SpecialMesh") or Asset:IsA("Animation") or Asset:IsA("ImageLabel") or Asset:IsA("ImageButton") or Asset:IsA("Sound") or Asset:IsA("Shirt") or Asset:IsA("Pants") or Asset:IsA("Decal") or Asset:IsA("Texture") then
		table.insert(needtobeloaded,Asset)
	end
end

local loadables = {}
for Id,Asset in pairs(table.unpack(assets):GetDescendants()) do
	if Asset:IsA("Decal") or Asset:IsA("Texture") or Asset:IsA("Shirt") or Asset:IsA("Pants") or Asset:IsA("ImageLabel") or Asset:IsA("ImageButton") then
		table.insert(loadables,Asset)
	end
end

can you post the error and also would it be better to use ‘GetChildren’ as descendants will get every single part which makes up an asset get children will only get the immediate children

there is no errors. It just loads the first Service in the “assets” table

table.unpack(assets):GetDescendants() is going to be calling GetDescendants() on the first item that gets unpacked which will be the first index.

Try something like this:

local assets = {
    game:GetService("StarterGui"),
    game:GetService("ReplicatedStorage"),
    game:GetService("Workspace")
}

local needtobeloaded = {}
local loadables = {}

for i, Service in pairs(assets) do
    for j, Asset in pairs(Service:GetDescendants()) do
        if Asset:IsA("MeshPart") or Asset:IsA("BlockMesh") or Asset:IsA("SpecialMesh") or Asset:IsA("Animation") or Asset:IsA("ImageLabel") or Asset:IsA("ImageButton") or Asset:IsA("Sound") or Asset:IsA("Shirt") or Asset:IsA("Pants") or Asset:IsA("Decal") or Asset:IsA("Texture") then
            table.insert(needtobeloaded,Asset)
        elseif Asset:IsA("Decal") or Asset:IsA("Texture") or Asset:IsA("Shirt") or Asset:IsA("Pants") or Asset:IsA("ImageLabel") or Asset:IsA("ImageButton") then
            table.insert(loadables,Asset)
        end
    end
end

Or perhaps more cleanly written:

local Services = {
    "StarterGui",
    "ReplicatedStorage",
    "Workspace"
}

local AssetTypes = {
    "MeshPart",
    "BlockMesh",
    "SpecialMesh",
    "Animation",
    "ImageLabel",
    "ImageButton",
    "Sound",
    "Shirt",
    "Pants",
    "Decal",
    "Texture"
}

local Assets = {}

for i, ServiceName in pairs(Services) do
    local Service = game:GetService(ServiceName)

    for i, Descendant in paris(Service:GetDescendants()) do
        if (table.find(AssetTypes, Descendant.ClassName)) then
            table.insert(Assets, Descendant)
        end
    end
end

Or Maybe you should put all of your content that needs to be replicated into some folder and then preload everything inside that folder so you don’t have to do 999 comparisons to see if something is of “loadable” type.

2 Likes

thank you. I tried the first method and now I see why it wouldn’t work. I always used assets:GetChildren :confused: