Images not being set?

Here is a script I created, and the images never change. I used print statements to make sure the if end was working properly, and it does.

if difference >= 5 then
			if difference >= 20 then 
				if difference >= 100 then
					image = listOfIds[4]
				else	
					image = listOfIds[3]
				end
			else
				image = listOfIds[2]
			end
		else
			image = listOfIds[1]
		end

Below is 'ListOfIds"

local listOfIds = {
	"rbxassetid://7456445734", 
	"rbxassetid://7459708647", 
	"rbxassetid://7459706335", 
	"rbxassetid://7459697091",
}

It doesn’t provide an error. :expressionless:

I also know this isn’t the best way to write the code, I am just trying to make it work.

You probably didn’t set the .Image property of the images. Try replacing all of your image = listOfIds into image.Image = listOfIds

I would recommend preloading your images using PreloadAsync().

local animator = script.Parent.Parent.Animator
local image = animator.Image

Doesn’t PreloadAync() just work for 1 image? (meaning that it loads 1)

PreloadAsync() loads every instance in a table and their descendants (so pretty much infinite).

Also you could fix the image problem by putting animator.Image = image at the end of your code.

No, you can preload assets like this.

local Images = {} -- Table of images
local ContentProvider = game:GetService("ContentProvider")
local ImagesLeft = #Images

ContentProvider:PreloadAsync(Images, function()
   ImagesLeft = ImagesLeft - 1
end)

if ImagesLeft == 0 then
   -- Code
end

I am not sure why this solved the problem, because ā€œimageā€ is basically ā€œanimiator.Imageā€

Does ā€˜#’ Return the length of a list? it seems to give an error to me so I use table.getn().

Yep, # returns the number of items in a table.

1 Like

You did local image = animator.Image which saves the current image of the animator in a variable. If you wanted to change the image, then you would have to change the .Image of the animator. Changing local image would just save a different number/variable instead to that variable.

Yes, # returns the length of indexs in a list. However, this does not include anything other than integers. So
local tab = {"Hi", "Pie", [3] = "Apple"} print(#tab) would print 3 whereas
local tab = {Apple = true, [1.5] = "pie", ["Workspace"] = 2} print(#tab) would print 0

table.getn() does exactly the same thing as # but slower.

1 Like