Random Image not working with imagelabel

I’m trying to make a script that loads a random image show up when you press to load into a game.
Here is the script I am using.

local imageIds = {"rbxassetid://myimageid1","rbxassetid://myimageid2","rbxassetid://myimageid3"}
local randomImage = imageIds[math.random(1,#imageIds)]
script.Parent.Image = randomImage

I was trying to make it show random images when ever it loads you into a different game

This is what it appears as in game


it should show up as this and similar images

I’ve tried doing rbxassetid:// before all the image ids but that doesnt work, And I looked on the creator hub, and found similar issues but nothing seemed to help!

So I don’t understand why it isnt working, and it gives no errors. So I can’t find the reason it does not work.

1 Like

Yo. I would recommand you to maybe use a for i, v in ipairs etc. loop to go throught the different images. Hope this help.

seems to work properly.
lemme change a few things >;3

Normal Version:

local imageIds : {number} = {139093553695913,96611340724735,74854335449470};
local assetId : number = imageIds[math.random(1,#imageIds)];
local image:string = "rbxassetid://"..assetId;

-- local script needs to be inside the image label.
script.Parent.Image = image;

Folder Version:

-- == LOADING == --

local ImageFolder : Folder = script.Images;
local imageIds : {string} = {};

for _, v in ImageFolder:GetDescendants() do
	if v:IsA("Decal") then
		table.insert(imageIds, v.Texture);
	end;
end;

-- == ASSIGN THE IMAGE == --

local assetId : string = imageIds[math.random(1,#imageIds)];

-- local script needs to be inside the image label.
script.Parent.Image = assetId;

Huh???
Why do you need to look through all the images?
To check if they’re valid?

nil

I want to use a loop so that the images appear one after the other for 5 seconds each, for example to create the loading effect

Hopefully this works for you :3

table.freeze turns a table into read-only mode, meaning they cannot be changed.

local Constants = table.freeze({
	IMAGE_SWITCH_TIME = 5,
	IMAGE_IDS = {139093553695913,96611340724735,74854335449470}
});

local stop : boolean = false;

local imageIds : {number} = Constants.IMAGE_IDS;
local assetId : number = imageIds[math.random(1,#imageIds)];

task.spawn(function()
	task.wait(13) -- TESTING
	stop = true;
end);

while task.wait(Constants.IMAGE_SWITCH_TIME) do
	if(stop)  then
		break; -- kill the while loop
	end;
	
	assetId = math.random(1,#imageIds);
	assetId = imageIds[assetId];
	
	-- you can add sum tweens there if you want... 
	
	script.Parent.Image = "rbxassetid://"..assetId;
end

:3

sorry if what I was saying wasn’t very clear, it’s because I only started coding 6 months ago, so I still lack a lot of knowledge :sweat_smile:

by the way, you’re really good at coding, I hope I’ll be able to reach your level one day!

1 Like

don’t worry, it’s fine. :3c

Anyways, I hope that script is what you wanted. ^ - ^

-orbl

1 Like

the script is perfect!!! love it!!!

1 Like

I don’t know if I did it wrong but when I tried that it still didn’t work even though I put the localscript inside imagelabel.

which script are you talking about?

local imageIds : {number} = {139093553695913,96611340724735,74854335449470};
local assetId : number = imageIds[math.random(1,#imageIds)];
local image:string = "rbxassetid://"..assetId;

-- local script needs to be inside the image label.
script.Parent.Image = image;```

hmm, it seems to be working for me…
Can you send me a screenshot of the layout?

My layout:

Ok this is what it looks like for me (Edit: I think the double quotes might mess it up but it didn’t give any errors with or without, It just showed up as white when I joined the game.)

either change the strings inside the list imageIds to numbers (remove the ")
or change the type {number} to {string}

fixed script:

local imageIds : {string} = {"496782421", "8304400127", "190389549"}; -- hopefully i got these correctly
local assetId : number = imageIds[math.random(1,#imageIds)];
local image:string = "rbxassetid://"..assetId;

-- local script needs to be inside the image label.
script.Parent.Image = image;

hope this help ^ - ^

Okay I’ll check if that works. Because the image keeps saying its not loaded.
Edit: I checked the image in the playergui and it showing this
Isloadedfalse

1 Like

You could either make a loading screen, or you could place the image labels or decals of the image into a folder and make them invisible.

Or you could use the second script instead, which automatically grabs the decals from a folder and pushes them into a list.

-- == LOADING == --

local ImageFolder : Folder = script.Images;
local imageIds : {string} = {};

for _, v in ImageFolder:GetDescendants() do
	if v:IsA("Decal") then
		table.insert(imageIds, v.Texture);
	elseif v:IsA("ImageLabel") then
		table.insert(imageIds, v.Image);
	end;
end;

-- == ASSIGN THE IMAGE == --

local assetId : string = imageIds[math.random(1,#imageIds)];

-- local script needs to be inside the image label.
script.Parent.Image = assetId;

I guess I’ll do the folder thing because I was going to make the image off screen and when you load into a game it will drop down before it loads the player into the game.
Thank you!

1 Like

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