PreloadAsync not actually preloading image ids

I have 149 image labels that are used to create an animated intro. However, there are times where frames are being skipped, I believe due to these images not actually being loaded. However, I preload all the images first before doing the animation, and there are some times where image does not load at all

-- Preload Everything
for i = 1, #PreloadAssets do
	ContentProvider:PreloadAsync(PreloadAssets[i])
	UpdateLoadingValue(i / #PreloadAssets)
	print(i / #PreloadAssets)
end
print("Intro assets have loaded successfully")

-- Initializing all frames for the animation sequence
local AnimationBufferIndices = 30 -- How many frames are onscreen at once to guarantee visibility
local IntroAnimationImages = table.create(#Ids, nil)
for i, imageId in ipairs(Ids) do
	local Img = LoadingUI.Animation.ImageTemplate:Clone()
	
	if i <= 50 then
		Img.Parent = LoadingUI.Animation
	else
		Img.Parent = nil
	end
	
	Img.Position = UDim2.fromScale(1.5, 0.5)
	Img.Image = imageId
	Img.Visible = true
	
	-- Storage
	IntroAnimationImages[i] = Img
end

-- this is the loop that does the animation
for i, image in ipairs(IntroAnimationImages) do
	image.Position = UDim2.fromScale(0.5, 0.5)
	
	-- Hide Last Frame
	if PrevImage then
		PrevImage.Position = UDim2.fromScale(1.5, 0.5)
		PrevImage:Destroy()
	end
	print(i, image)
	PrevImage = image
	
	task.wait(AnimationSpeed)
end

You’re likely queueing too many images to be preloaded, look into using sprite sheets instead.

If any assets failed to load, PreloadAssets should announce this in the Output. If you got no output, then there’s a chance you weren’t preloading anything.

While we can’t see your PreloadAssets table, this could have something to do with it.

If your PreloadAssets table consists only of ID numbers, it’s not preloading anything. For example:

http://www.roblox.com/asset/?id=7519451 will work, 7519451 will not, unless your for loop concatenates the http://www.roblox.com/asset?id= in front.

This is just a guess based on the behavior of ContentProvider:PreloadAsync more than the code you’ve provided here.

The number of assets you attempt to preload at once shouldn’t be a problem, because it yields until it’s finished.

You can time your preloading efforts like this:

local startTime = os.clock()
ContentProvider:PreloadAsync(assets)
local deltaTime = os.clock()
print(("Preloading complete, took %.2f seconds"):format(deltaTime))

If it happens instantly, then you know for certain that your code didn’t actually attempt to preload your assets.

image
This is what’s being preloaded. I assume PreloadAsync preloads image ids??

I also segment them into seperate chunks, so preloading 140+ images a single table takes a considerably longer time than a table of 20. Once the images have been preloaded, I create all the imagelabels, store them, then after loop through them to create a slideshow affect. Unsure if this is the best approach, if I should be just creating 1 imagelabel and changing it’s id rapidly. I feel that would be worse tho, as Imagelabels take a split second to load their images it seems, but idk :man_shrugging:

The images are 100% being added to the preload table, and are 100% being preloaded, as I’ve added prints to confirm this + it yields. It does 8 tables (7 with 20, last one with 9, so total of 149 image ids) and as mentioned earlier, breaking them up into smaller tables made preloading them a lot faster over just preloading the entire 149 image id table (that took up to 50-60 seconds to preload them all, can’t have that) while the shortened table takes about 5-6 seconds, depending on connection.

local PRELOAD_ASSET_BUFFER_SIZE = 20

local PreloadAssets = {[1] = {}}
local PreloadAssetIndex = 1
local function AddAsset(assetId)
	-- Add
	table.insert(PreloadAssets[PreloadAssetIndex], assetId)
	
	-- Max Size, increment counter
	if #PreloadAssets[PreloadAssetIndex] == PRELOAD_ASSET_BUFFER_SIZE then
		PreloadAssetIndex += 1
		PreloadAssets[PreloadAssetIndex] = {}
	end
end

for _, assetId in ipairs(Ids) do
	AddAsset(assetId)
end

-- Preload Everything
for i = 1, #PreloadAssets do
	ContentProvider:PreloadAsync(PreloadAssets[i])
end

-- Create the image labels
for i, imageId in ipairs(Ids) do
	local Img = LoadingUI.Animation.ImageTemplate:Clone()
	Img.Parent = LoadingUI.Animation
	
	Img.Position = UDim2.fromScale(1.5, 0.5)
	Img.Image = imageId
	Img.Visible = true
	
	-- Storage
	IntroAnimationImages[i] = Img
end

-- Do the animation
local AnimationSpeed = 1 / 42
local PrevImage = nil
local BufferRefilCheck = 10
local BufferRefilIndex = AnimationBufferIndices + 1

for i, image in ipairs(IntroAnimationImages) do
	image.Position = UDim2.fromScale(0.5, 0.5)
	
	-- Hide Last Frame
	if PrevImage then
		--PrevImage.Position = UDim2.fromScale(1.5, 0.5)
		PrevImage:Destroy()
	end
	
	PrevImage = image
	
	-- Check if approaching buffer limit
	if i + BufferRefilCheck == BufferRefilIndex then
		-- Start reparenting more frames
		local NewMax = math.min(BufferRefilIndex + AnimationBufferIndices, #IntroAnimationImages)
		
		for i = BufferRefilIndex, NewMax do
			IntroAnimationImages[i].Parent = LoadingUI.Animation
		end
		
		BufferRefilIndex = NewMax + 1
	end
	
	task.wait(AnimationSpeed)
end

PreloadAsync yields the thread until the given item(s) have loaded. Using that knowledge, that’s likely the reason for some items not loading.

Another thing is that instead of looping through it, you can actually just set a table for it, such as this:

local ContentProvider = game:GetService('ContentProvider')

ContentProvider:PreloadAsync(script.Parent:GetChildren()) -- Example.

Hopefully this helps you! :smile:

You have not read my post at all :roll_eyes:

Please read my post and further comments before giving an answer, as I clearly know how PreloadAsync works

I’m not too sure why this is happening. But maybe wrap your preloading part inside a pcall?

Again, PreloadAsync yields the thread until the item has loaded. If the item yielding never loads, it can result in the rest never loading.

Also, don’t rely on PreloadAsync to load your loading screen, utilize ReplicatedFirst.

You are still understanding what my problem is. I know PreloadAsync yields, I clearly say it right here

How would ReplicatedFirst fix any of this!? The script is in ReplicatedFirst, I have 149 ids that need to laod. I’m not gonna manually make all them into imagelabels and do that. As I have said numerous times, ALL 149 images are LOADING! So Preload is ‘working’. When I apply their ids to an ImageLabel however, they do not work and load instantly

Any solution yet? I’ve seen countless other posts like this one, and I’m convinced that it just doesn’t work at all (for images, at least).

1 Like