Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix "Unable to cast to Array" error?

Hello,

The code is in a LocalScript located in the StarterGui and whenever I click play the output fills with "Unable to cast to Array". I don't understand it what it means but if you do that would help a lot!

12:57:32.436 - Unable to cast to Array

local contentProvider = game:GetService("ContentProvider")

for i,service in pairs(game:GetChildren()) do
		pcall(function()			
			wait(.1)
			script.Parent.Parent.loading.loadingBars.assets.Text = i.." assets loaded"
			contentProvider:PreloadAsync((service))
		end)
	end

1 comment

0

Try changing the last part of line 7 from 'PreloadAsync((service))' to 'PreloadAsync({service})'

1 answer

✓ Accepted answer

Hmmm... Not sure, I've used this before and it worked perfectly fine.

Here's another way of doing it, it's just more complicated. Not really sure why it's throwing this error.

local ContentProvider = game:GetService("ContentProvider")

local assets = {
	game:GetService("Workspace"),
	game:GetService("Players"),
	game:GetService("Lighting"),
	game:GetService("ReplicatedFirst"),
	game:GetService("ReplicatedStorage"),
	game:GetService("ServerScriptService"),
	game:GetService("ServerStorage"),
	game:GetService("StarterGui"),
	game:GetService("StarterPack"),
	game:GetService("StarterPlayer"),
	game:GetService("Teams"),
	game:GetService("SoundService"),
	game:GetService("Chat"),
	game:GetService("LocalizationService"),
	game:GetService("NetworkClient")
}

for i = 1,#assets do
	local assetToLoad = assets[i]
	ContentProvider:PreloadAsync({assetToLoad})
	local newX =  i / #assets
	script.Parent.Parent.loading.loadingBars.assets.Text = "Loading Assets: "..math.floor (i / #assets * 100).."%"
end

The only difference is I manually listed the assets I wanted to load in.

This one should work, as I've tested it before. If this seems to not work, let me know and I'll fix it for you.

Hope this helped!

2 comments

0

awesome, thanks!

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.