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)
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
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:
Use ContentProvider:PreloadAsync - Untested. To preload each image beforehand (before your loading loop)
Set it to one image and just adjust the Rotation property of the ImageLabel
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.
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()
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 - "
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