How to wait for image to load on the client?

I’m currently working on a way to change multiple images at once with Decal IDs, the issue is that the images aren’t loaded when they appear… how do I fix this?

There are also times where the game will lag when the image changer is on.

Local Script

ForDevForum.rbxl (75.6 KB) <— Updated

local Titantrons = {}
for i,v in pairs(script.ScreenValues:GetChildren()) do
	repeat wait() until game.Workspace:FindFirstChild(v.Value)
	if game.Workspace:FindFirstChild(v.Value) then
		Titantrons[v.Value] = game.Workspace[v.Value].Decal.Changed:Connect(function(ValueChanged)
			if ValueChanged == "ChangedTexture" or ValueChanged == "Texture" then
				local id = string.split(game.Workspace[v.Value].Decal.Texture,"http://www.roblox.com/asset/?id=")[2] or string.split(game.Workspace[v.Value].Decal.Texture,"rbxassetid://")[2] or game.Workspace[v.Value].Decal.Texture
				local Stages = game.Workspace:FindFirstChild("Stages") or game.Workspace
				for index,Item in pairs (Stages:GetDescendants()) do
					if Item:IsA("BasePart") and string.lower(Item.Name) == (string.lower(v.Value).."copy") then
						if Item:FindFirstChild("Decal") then
							Item.Decal.Texture = "http://www.roblox.com/asset/?id="..id
						elseif Item:FindFirstChild("SurfaceGui") then
							Item.SurfaceGui.ImageLabel.Image = "rbxassetid://"..id
						end
					end
				end
			end
		end)
	end
end
5 Likes

So, I believe what you would want to do to load all your decals is by using ContentProvider:PreloadAsync(), and just inputting all the decals and stuff you want to preload. Make sure you put the preloading script in a local script, and then put that local script in ReplicatedFirst.

You could do something like this and it should work:

local ContentProvider = game:GetService("ContentProvider")

ContentProvider:PreloadAsync() -- in the parentheses put a table of all the image IDS you want to preload
2 Likes

The issue with this, is that any and all images are not made by me, or in the game before the server starts… some of these will be added while playing the game… I’m not sure it would work with this.

3 Likes

So you are creating new decals via Instance.new() partway through the game? If so, you could just create a folder that has all the images you need that you will get later, preload those, and then when you clone them they should already be loaded.

2 Likes

Also, just to clarify, the images are loading but they are just delayed before they appear? Or are they not loading at all

2 Likes

No, I’m changing one Decals image constantly and transmitting it’s change across multiple other Images/Decals across the map… none of the images are inserted… and I don’t have much control over it either.

Essentially, there are times where an image will end up not showing up and since these images change with at the fastest being .01… I need to make it so only the loaded images appear… instead of the nonloaded ones appearing and showing a momentary black screen/stutter screen. Here’s a video to shown what I mean. ← You can see in the beginning that the DTA stutters… then loads and plays normally until it goes to the other parts.

2 Likes

I believe you can still preload images without physically having the asset existent. You just need to preload the image ids in a format like this:

-- LocalScript
local function checkFailed(contentId, Status)
   if Status == Enum.AssetFetchStatus.Failure then
       print("Failed to load", contentId)
   end
end

ContentProvider:PreloadAsync(guiImagesModule.images, checkFailed)

-- Module
local images = {}
images.images = {
     "http://www.roblox.com/asset/?id=6159680612",
     "http://www.roblox.com/asset/?id=6180336941"
}
return images

You just need to have the image IDS in a table (not as a dictionary or anything) and just send the table as the thing you want to preload. You can even do this without a module script and just create a table with all the image IDS you want to preload.

2 Likes

Also, after checking the file you sent of your game, some of the lag could also be due to the fact you are using wait() instead of task.wait(). wait() can be known to cause lag and bugs in some ways, and task.wait() is superior in almost every aspect.

2 Likes

Could you elaborate on which script has the wait you’re referring to?

1 Like

1 Like

yeahh, sadly I’m not allowed to change it. It’s what the community I’m apart of uses and the community refuses to upgrade.

1 Like

What community is this? Because after reading some more of these scripts, I can see why a lot of this isn’t working. Everything is set up in like, the least efficient way possible. If the community doesn’t want you to change it, then that’s fine, but I don’t know what more I could do to help. Getting this to work would take a rework of most of the systems

1 Like

Roblox Wrestling, the community isn’t savory; to put it lightly. Most groups use tech from 8 years ago, or as the script you see… 12 to 13 years ago. I’m slowly working towards a reworked system… but if I can’t separate whether an image is loaded or not… then there really isn’t a point of trying a rework.

1 Like

Ah, I see. That checks out. I’m gonna go test a few things to see if I can help with the loading problem with the systems as it is, and I’ll let you know if I found anything that works.

2 Likes

Alright, I think I found the solution. It will take a long time to set up depending on how you currently have things organized (or are reorganizing), but it should work.

You will want to make a local script in replicated first and do something like this:

local ContentProvider = game:GetService("ContentProvider")

local IDS = {
	"http://www.roblox.com/asset/?id=2675785344",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6266766341",
	"http://www.roblox.com/asset/?id=6273789895",
	"http://www.roblox.com/asset/?id=6264447407",
	"http://www.roblox.com/asset/?id=6264448504",
	"http://www.roblox.com/asset/?id=6264450933",
	"http://www.roblox.com/asset/?id=2675785344",
	"http://www.roblox.com/asset/?id=6255989403",
	"http://www.roblox.com/asset/?id=6255989836",
	"http://www.roblox.com/asset/?id=6255990255",
	"http://www.roblox.com/asset/?id=6255990700",
	"http://www.roblox.com/asset/?id=6255991107",
	"http://www.roblox.com/asset/?id=6255992210",
	"http://www.roblox.com/asset/?id=6255993954",
	"http://www.roblox.com/asset/?id=6255994471"
-- this is the amount of IDS I tested, but you will need to put all of them in here.
}

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

ContentProvider:PreloadAsync(IDS, checkFailed)

I tested it, and it did preload the images in the table. The TV part the images were supposed to change on reacted much sooner than they did the first time, and there was little to no blacking out for the selected images to save. Now do keep in mind that because you have so many images to preload, you might need to create multiple tables and preload each table separately once you hit the max object limit in a table. I hope this helps!

Edit: alternatively, you could just make the check function do it for every part that has a decal named (whatever you want it to be) and then just make it change the decal of each of those.

Edit edit: After doing just a little more research and testing, I don’t think it’s actually possible to change the decal on 20+ parts every 20th of a second. Roblox itself can’t handle things changing that fast, even if it’s preloaded. You are probably better off making an animation of what you want to show, then using a ViewportFrame to display it. Sorry for all the confusion.

1 Like

I had a feeling this might be the case; I just want to make it where the screens will (instead of going to an not loaded image) it’ll just skip to the next image.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.