Unable to cast to array

I’m trying to use Content Provider to preload all images in a module script. And when I try, it says Unable to cast to array.

local function PreloadFailed(contentId, Status)
	if Status == Enum.AssetFetchStatus.Failure then
		error("Failed to load", contentId)
	end
end

task.spawn(function()
	ContentProvider:PreloadAsync(Images, PreloadFailed)
end)

Does anybody know how to fix this? Or, a better way to preload all assets in a modulescript?

Copy and paste this into your script.

local function PreloadFailed(contentId, Status)
	if Status == Enum.AssetFetchStatus.Failure then
		error("Failed to load: "..tostring(contentId)) -- Try this
	end
end

task.spawn(function()
	ContentProvider:PreloadAsync(Images, PreloadFailed)
end)

Sadly, that did not work. I have a feeling it has something to do with the fact I am trying to preload out of a module, and not a folder of some sort. (I’m trying to preload out of a module for optimization reasons.)

Which line is giving that error? If it’s PreloadAsync, that probably means that Images isn’t an array.

It’s this line:

ContentProvider:PreloadAsync(Images, PreloadFailed)

PreloadAsync requires a Table to work, Try this:

ContentProvider:PreloadAsync({Images, PreloadFailed})

or this (If it works):

ContentProvider:PreloadAsync{Images, PreloadFailed}

ContentId is asking for an array of objects to be Preloaded, an array is a table in this case.

Content = {
   Item1 = workspace.Baseplate
   Item2 = workspace.SpawnLocation
}

CProvider:PreloadAsync(Content) -- Preloads Items in Array

What NOT to do:

Item1 = workspace.Baseplate

CProvider:PreloadAsync(Item1) -- This will error

That is why you are getting the Error: Unable to cast to Array
Because there is no Array.

Oh, that makes sense. My end goal is to preload all the items in a module. I’m just referring to the module through require() if that makes sense. Is there any better way or any way to preload everything in a module without having to make an array in the local script?

By “everything,” do you mean this?

for _,i in pairs({workspace:GetDescendants()}) do -- Goes through a Table with a Table
   CProvider:PreloadAsync(i) -- Preloads Table with Descendants of the workspace
end

GetDescendants() returns a Table of all Descendants in the hirearchy

Ancestor – Parent of Parent (All Parents of the Instance and so on)
Parent – Instances Parent
Instance – Your Item
Child – Instances Children
Descendant – Children of Children ( All Items within the Instance )

you can do game:GetDescendants() but you might get errors for Accessing Locked Service’s

If I used that on a folder containing ModuleScripts. Would it preload all the data inside the modulescripts?

It should, if it is one of the Descendants.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.