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)
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.
--//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
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…