PreloadAsync does not work

Hi everyone! I’m trying to use PreloadAsync to preload some ImageLabels from my game, but it does not work, both in studio and client.
I want to preload some images that need to be showed at the exact moment the player click that, but at the first time it takes some time to download the images, so like, if I want to tween the transparency of an image, to show it gradually, it won’t work, as it would just pop up when the image is downloaded and the tween is over.
I’ve suffering from this problem since I started developing, and I already searched on the forum, but nothing managed to solve my problem.

I got no idea if I’m using PreloadAsync wrong, but it is not working and never worked for me…
Here’s the script that try using:

--LocalScript at ReplicatedFirst
local ContentProvider = game:GetService("ContentProvider")
local ImagesModule = require(script.Images)
local function checkFailed(contentId, Status)

if Status == Enum.AssetFetchStatus.Failure then
print("Failed:", contentId)
end
end

ContentProvider:PreloadAsync(ImagesModule.images, checkFailed)
--ModuleScript inside the LocalScript
local images = {}
images.images = {
"rbxassetid://10536397078",
"rbxassetid://10536435527",
"rbxassetid://10536440796",
"rbxassetid://10529599606",
"rbxassetid://10479528722",
"rbxassetid://10629433764",
"rbxassetid://10629445663",
"rbxassetid://10629439770",
"rbxassetid://10629425016",
"rbxassetid://10502315297",
"rbxassetid://10502345596",
"rbxassetid://10502349157",
"rbxassetid://10491070536"
}
return images

no errors in the output

1 Like

You have to preload async an actual table of instances. Before it used to be a table of ids though, but they changed it.


(the name is kinda misleading but the description explains it)

1 Like

Could you explain a bit more? I suck at coding…

You can just preload the PlayerGui:GetDescendants(), I’ll write some code rn.

1 Like

I’m not sure if I got it right, it would be something like this?

local images = {}
images.images = {
local Image1 = Instance.new("Image1"),
Image1.Image = "rbxassetid://10536397078",
--etc...

You don’t have to change the code of the module, just switch your script to this:

--//Services
local ContentProvider = game:GetService("ContentProvider")

--//Modules
local ImagesModule = require(script.Images)

--//Functions
local imagesToLoad = {}

for i, imageId in ipairs(ImagesModule.images) do
	local newImage = Instance.new("ImageLabel")
	newImage.Image = imageId
	
	table.insert(imagesToLoad, imagesToLoad)
end

local function checkFailed(contentId, Status)
	if Status == Enum.AssetFetchStatus.Failure then
		print("Failed:", contentId)
	end
end

ContentProvider:PreloadAsync(imagesToLoad, checkFailed)

for i, image in ipairs(imagesToLoad) do
	image:Destroy()
end

This will cache the image data, therefore allowing the client to instantly retrieve it when needed.

1 Like

I think I got it now, but for some reason it says that tables cannot be cyclic at this line:

Oops I inserted the wrong value.

Try this:

--//Services
local ContentProvider = game:GetService("ContentProvider")

--//Modules
local ImagesModule = require(script.Images)

--//Functions
local imagesToLoad = {}

for i, imageId in ipairs(ImagesModule.images) do
	local newImage = Instance.new("ImageLabel")
	newImage.Image = imageId
	
	table.insert(imagesToLoad, newImage)
end

local function checkFailed(contentId, Status)
	if Status == Enum.AssetFetchStatus.Failure then
		print("Failed:", contentId)
	end
end

ContentProvider:PreloadAsync(imagesToLoad, checkFailed)

for i, image in ipairs(imagesToLoad) do
	image:Destroy()
end
1 Like

Thank you so much man! I finally managed to see PreloadAsync working!

1 Like

Just discovered that it still doesn’t work. I thought it worked but it was just some cached memory from the first time I tested the game, so yeah, sadness and pain…

Do you have the actual images stored at the start of the game? You could try preloading those in.