How can I grab a specific item in a table defined by the number of order they are in?

Hi!
I am trying to achieve grabbing a specific item from a table defined by the order they are in.
For example

local list = {
12213, -- This is number 1 as it is first on the list.
123115, -- This is number 2 as it is second on the list.
}

I am trying to grab a specific one based off of the variable “num”. Like if I did script.Parent.Image = “rbxassetid://”…[num]… it would set it to 12213 since that is the first one on the list, so on so fourth.

I have tried to experiment with this, I mixed the way up with JavaScript but I don’t know the way to grab it.

Code:

local images = {
	12433575539,
	12433580610, -- 2
}

while true do
	local num = 0
	script.Parent.Image = "rbxassetid://"..images[num]
	num += 1
	wait(0.01)
end

This is me trying to make a progressive loading screen where the thing spins in circles. (ive manually uploaded the images)

1 Like
local images = {
	12433575539,
	12433580610, -- 2
}

local num = 1 -- define the number outside of the loop so it isn't being redeclared each iteration
-- Lua/Luau tables start at 1, so you'd want the initial value to be 1

while true do 
    if num > #images then -- '#' is the length operator; it returns the length of a string or a table
        break -- end the loop since there are no more images in the table
    end

	script.Parent.Image = "rbxassetid://"..images[num]
	num += 1
	task.wait()
end
2 Likes

Ok well, first of all, in Lua, indexes start at 1.

So you should set num to 1 first.

Second, are there more images in your real code? Because there are only two here and after the 2nd image, they will all always be nil.

Try num = num % #images + 1

Finally, loading images one after another like that may take a while before they actually load in, and as the delay you use is very short, the first time around none of the images will actually load.

To solve this tou have two options:

  1. Use ContentProvider:PreloadAsync - Untested. To preload each image beforehand (before your loading loop)
  2. Set it to one image and just adjust the Rotation property of the ImageLabel
2 Likes

Okay, I will try that out now! What is the difference between wait() and task.wait()?

1 Like

task.wait resumes at 60 Hz while wait resumes at 30 Hz

1 Like

Ooooh, so is task.wait less of a wait time?

You can sort of think of it that way since the minimum wait time with task.wait is 1/60, while wait is 1/30.

task.wait is essentially supposed to be more accurate since the client usually runs at 60 Hz

2 Likes

I tried it, but the images appear as blank. Idk if it’s the images not approved (which is weird since they are both approved)
image

You can use @majdTRM’s solution since mine stops the loop once it hits the maximum value

1 Like

I think it is as I mentioned before (I edites my comment, so maybe that’s why you didn’t see it)

When you first go through the loop, you set the images fresh. Roblox has not downloaded these images yet and tries to download them.

By the time they’re downloaded, you’ve already set the next image. You probably want to run each image through ContentProvider:PreloadAsync first before you run the loop.

1 Like

Ooooh, okay! I will try that now. Thank you both for it!

1 Like

Would I do ContentProvider:PreloadAsync([imageid]) to do that? Or could I just do the whole list in one line? I have a total of 100+ images.

1 Like

I think you should create an array of decals (yes, I know, it’s weird) and pass the entire array to PreloadAsync.

Then, it will yield (stop the thread) until they all load in and then start the loop.

local list = { 123, 456, 789, ... }
function preloadAll()
    local decals = {}
    for _, id in list do
        local decal = Instance.new("Decal") -- You don't *need* to use Decals, I think you can use ImageLabels as well but I haven't tested.
        decal.Texture = id
        table.insert(decals, decal)
    end
    ContentProvider:PreloadAsync(decals) -- You need to set ContentProvider first via game GetService
end

preloadAll()

Untested but you can try.

1 Like

I get this error " 17:16:55.345 Unable to cast to Array - Client - LocalScript:7
17:16:55.345 Stack Begin - Studio
17:16:55.345 Script ‘Players.King_GamingRobloxYT.PlayerGui.SpinningLoading.Frame.ImageLabel.LocalScript’, Line 7 - "

Can you show me how you types it in the script?.

I figured out the error.
It was because I had a previous one that would cause an error.

But the images still appear as blank.
image

local images = {
	12433575539,
	12433580610, -- 2
	12433583118, -- 3
	12433583822, -- 4
}

local num = 1 -- define the number outside of the loop so it isn't being redeclared each iteration
-- Lua/Luau tables start at 1, so you'd want the initial value to be 1


local ContentProvider = game:GetService("ContentProvider")
function preloadAll()
	local decals = {}
	for _, id in images do
		local decal = Instance.new("Decal") -- You don't *need* to use Decals, I think you can use ImageLabels as well but I haven't tested.
		decal.Texture = id
		table.insert(decals, decal)
	end
	ContentProvider:PreloadAsync(decals) -- You need to set ContentProvider first via game GetService
end

preloadAll()

while true do 
	if num > #images then -- '#' is the length operator; it returns the length of a string or a table
		num = 1  -- end the loop since there are no more images in the table
	end

	script.Parent.Image = "rbxassetid://"..images[num]
	num += 1
	task.wait()
end

Try waiting a little longer, change your task.wait to task.wait(3) just to sre if they load in at all.

Okay, I will be trying that out now to see if it works!

Still nope.
image
I don’t know why they keep doing this, I will check the image frame in the player gui tab.