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()
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.
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()