Loading Screen error

I’m making a functional loading screen, but whenever it finishes loading, it never closes the gui.

Code:

--note: it already replaces the default loading gui

--// Variables \\--
local loadingScreen = script.ScreenGui
loadingScreen.Parent = game.Players.LocalPlayer.PlayerGui
--// Loading \\--
local contentProvider = game:GetService("ContentProvider")
toLoad = game.Workspace:GetDescendants()

local total = #toLoad
for i,v in pairs(toLoad) do
	--code
	contentProvider:PreloadAsync((v)) --this is the line that the error redirects to
end
--// Finished Loading \\--
task.wait(1)
loadingScreen:Destroy()

The error:

Replace:

for i,v in pairs(toLoad) do
	--code
	contentProvider:PreloadAsync((v)) --this is the line that the error redirects to
end

with:

contentProvider:PreloadAsync(toLoad)
1 Like

PreloadAsync({v}) [Char Limits]

1 Like

that would just mess up the whole script, and the loading screen wouldn’t even load anymore

1 Like

Sorry, didn’t see that you have code here, use this instead

contentProvider:PreloadAsync(toLoad, function()
    
end)
1 Like

thanks!

characters limit 30 yes

2 Likes

This would mean you’re inefficiently processing the content to preload.
Since the ‘toLoad’ variable is an array of instances, you can easily use this solution. It gets rid of the redundancy of the pairs loop, and makes it easier to integrate your code from the loop.

1 Like

in my code its using the in pairs so I have to use

PreloadAsync({v})

as @ayoub50 said.

1 Like

This function will be fired each time asset is loaded so it should work the same.

1 Like

it still errors, im making a loading bar and a text that says what is loading, how would I fix this? should I show you all the code?

1 Like

Yes, that would be good.

char limit

1 Like

You misread what I said.

This function automatically iterates through your ‘toLoad’ array, saving the hassle of having to individually preload everything in the array using pairs (which, should be replaced with ipairs since it is an array, not a dictionary).
Furthermore, a more optimized and ‘cleaner’ way of handling the preloaded content would be something like this:

--note: it already replaces the default loading gui

--// Variables \\--
local loadingScreen = script.ScreenGui
loadingScreen.Parent = game.Players.LocalPlayer.PlayerGui
--// Loading \\--
local contentProvider = game:GetService("ContentProvider")
-- ex.
local toLoad = workspace:GetDescendants()

local total = #toLoad
local loaded = 0
-- Wrap this in a protected call.
local success, error = pcall(contentProvider.PreloadAsync, contentProvider, toLoad, function()
	-- code
	loaded += 1
	print("Loaded", toLoad[loaded])
end)
if not success then
	warn("Could not preload assets:", error)
end
--// Finished Loading \\--
task.wait(1)
loadingScreen:Destroy()
1 Like
--// Removing default loading screen \\--

script.Parent:RemoveDefaultLoadingScreen()

--// Variables \\--

local loadingScreen = script.ScreenGui

loadingScreen.Parent = game.Players.LocalPlayer.PlayerGui

--// Loading \\--

local contentProvider = game:GetService("ContentProvider")

toLoad = game.Workspace:FindFirstChild("Map"):GetChildren()

local total = #toLoad

for i,v in pairs(toLoad) do

loadingScreen.cs.LoadingName.Text = "loading: " .. i .. "/" .. total .. " " .. v.Name

loadingScreen.cs.BarBG.Bar.Size = UDim2.new(i/total, 0, 1, 0)

contentProvider:PreloadAsync({v})

end

--// Finished Loading \\--

task.wait(1)

loadingScreen:Destroy()
1 Like

character limit character limit

1 Like

You’d do something like this then:

--note: it already replaces the default loading gui

--// Variables \\--
local loadingScreen = script.ScreenGui
loadingScreen.Parent = game.Players.LocalPlayer.PlayerGui
--// Loading \\--
local contentProvider = game:GetService("ContentProvider")
-- ex.
local map = workspace:WaitForChild("Map")
local toLoad = map:GetChildren()

local total = #toLoad
local loaded = 0
-- Wrap this in a protected call.
local success, error = pcall(contentProvider.PreloadAsync, contentProvider, toLoad, function()
	loaded += 1

	loadingScreen.cs.LoadingName.Text = "loading: " .. loaded .. "/" .. total .. " " .. toLoad[loaded].Name
	loadingScreen.cs.BarBG.Bar.Size = UDim2.fromScale(loaded / total, 1)

	print("Loaded", toLoad[loaded])
end)
if not success then
	warn("Could not preload assets:", error)
end
--// Finished Loading \\--
task.wait(1)
loadingScreen:Destroy()
2 Likes

This should work:

--// Removing default loading screen \\--

script.Parent:RemoveDefaultLoadingScreen()

--// Variables \\--

local loadingScreen = script.ScreenGui

loadingScreen.Parent = game.Players.LocalPlayer.PlayerGui

--// Loading \\--

local contentProvider = game:GetService("ContentProvider")

toLoad = game.Workspace:FindFirstChild("Map"):GetChildren()

local total = #toLoad
local current = 0

contentProvider:PreloadAsync(toLoad, function()
    current += 1

    loadingScreen.cs.LoadingName.Text = "loading: " .. curent .. "/" .. total .. " " .. toLoad[current].Name
    loadingScreen.cs.BarBG.Bar.Size = UDim2.new(current / total, 0, 1, 0)
end)

--// Finished Loading \\--

task.wait(1)

loadingScreen:Destroy()
2 Likes

maybe it is because you put parentheses in parentheses in your:PreloadAsync()

1 Like

Oh, you were faster :joy:

char limit

2 Likes

doesn’t work.

char limit yes

1 Like

it works,

barely

whenever I join the game it only loads 2 parts and then closes the gui.

1 Like