Loading GUI not working?

I’m trying to make a loading screen gui with the assets that need to be loaded. I’m not going to even think about doing fake loading screens.

local function spinImage()
    while wait() do
        script.Parent.Frame.ImageLabel.Rotation = script.Parent.Frame.ImageLabel.Rotation +2
    end
end

local contentProvider = game:GetService("ContentProvider")
local toLoad = workspace:GetDescendants()
local total = #toLoad


spinImage()
for i,v in pairs(toLoad) do
    contentProvider:PreloadAsync(v)
    script.Parent.TextLabel.Text = "Loading Assets : " .. i .. "/" .. total
    end

I need it explained / fixed simply, to help others and myself.

your spinimage function is preventing ur for loop from running, move the spinImage function in a seperate thread with spawn() or coroutine

i deleted the spin thing since it was basically useless. 19:05:01.288 - Unable to cast to Array

19:05:01.289 - Stack Begin

19:05:01.290 - Script ‘Players.KryptonFoox.PlayerGui.LoadingGui.LocalScript’, Line 8

19:05:01.290 - Stack End i got this when i ran the code

The preloadasync function needs to be passed in a table, passing in a object will not work

Just like @Thedagz said, PreloadAsync() accepts an array in the parameter and what I can see from your code is that you only added one object, maybe you could do contentProvider:PreloadAsync(toLoad) because toLoad is an array contained with all of the descendants of the workspace so that should work.

game.ReplicatedFirst:RemoveDefaultLoadingScreen()

local contentProvider = game:GetService("ContentProvider")

script.Parent.Loading:Play()

while wait() do
	local toLoad = workspace:GetDescendants()
	local preload = contentProvider:PreloadAsync(toLoad)
	script.Parent.TextLabel.Text = "Loading Assets : " .. contentProvider.RequestQueueSize 
	if contentProvider.RequestQueueSize == 0 then
		script.Parent:Destroy()
		end
	end
	``` I got this script at the moment. I change things alot.

Forgot to mention, 19:18:34.807 - Players.KryptonFoox.PlayerGui.LoadingGui.LocalScript:10: attempt to index nil with ‘TextLabel’ is the output of it.

Here you are changing the Textlabel’s text but

here you have deleted the parent of the script so the TextLabel will error, could you show me your explorer?

try using a waitforchild script might run before text label has chance to load in fully

Use a pcall with the PreloadAsync. Always use pcall with things that could error.

Also, you can run a function for every time an instance gets loaded:

pcall(function()
    ContentProvider:PreloadAsync(game:GetChildren(), function() print(“Preloaded an Instance!”) end)
end)