How to Make Info Related to Randomly Picked Numbers from a Table

Pardon the confusing title, I really had no idea how to go about wording it. So, I am making this fun little machine that will give random images from my clan’s history when the lever is pulled. I managed to get it to give a random image every time the lever is pulled (it surprisingly worked my first time), but I am absolutely stumped as to how to solve the problem.

I wish to give each image a small text on a separate surfaceGUI. The issue is, I have absolutely no idea how to make it so that when it choses a random image, that it will also choose the text that is meant to go along with it. This is my first time scripting with tables, is there a way to make it so that it can randomly pick both the roblox image ID as well as the text I wish to go along with it?

Normally I’d try to come up with some ideas myself, but I’m quite honestly clueless on how to approach this.

The script for the machine:

local Screen1 = script.Parent.Parent.Screen1.SurfaceGui.ImageLabel
local Screen2 = script.Parent.Parent.Screen2.SurfaceGui.TextBox
local Screen3 = script.Parent.Parent.Screen3.SurfaceGui.TextBox

local FEARImages = {"6653345357","6653332249"}

function GetImage()
	local randomimage = "http://www.roblox.com/asset/?id="..FEARImages[math.random(1, #FEARImages)]
	Screen1.Image = randomimage
end

ClickDetector.MouseClick:Connect(function()
	Rotate.Disabled = false
	Rotate.Parent.Primary.Sound.Playing = true
	Screen3.TextTransparency = 0
	Screen3.Parent.Parent.SurfaceLight.Enabled = true
	Screen3.Parent.Parent.Computer:Play()
	Lever.Orientation = Vector3.new(-90, -135, 0)
	ClickDetector.MaxActivationDistance = 1
	wait(5)
	Rotate.Disabled = true
	Rotate.Parent.Primary.Sound.Playing = true
	Screen3.TextTransparency = 0.8
	Screen3.Parent.Parent.SurfaceLight.Enabled = false
	Lever.Orientation = Vector3.new(-45, -45, -90)
	Screen2.Text = "Enjoy your image!"
	
	GetImage()
	Screen3.Parent.Parent.Tada:Play()
	wait(2)
	Screen2.Text = "AWAITING INPUT"
	ClickDetector.MaxActivationDistance = 15
end)

Please ignore my lack of a debounce which was replaced by a change in MaxActivationDistance, I’ve been up for like 20 hours working on this game, lol. I’d be glad to explain more if needed, as I’m probably not being very clear here.

1 Like

One solution would be to put a String Value inside each image.
Then you can get the value of said String Value and make it the TextLabel’s text.

I’m not quite sure what you mean. How would I go about putting a string value in with each ImageID in the table?

Oh. Right, sorry about that.
I suggest you make tables within a table.

local FEARImages = {
{"6653345357", "Your Text Here"}, 
{"6653332249", "Your Text Here"} 
}

You can then index the text.

function GetImage()
    local selectedTable = FEARImages[math.random(1, #FEARImages)]
	local randomimage = "http://www.roblox.com/asset/?id="..selectedTable[1]
	Screen1.Image = randomimage
    [TextLabel].Text = selectedTable[2]
end

Oh, I was not aware I could have this table-ception, thanks! I will give this a try.

Edit: It worked just fine, thank you for the help. Being my first time using tables, this certainly helped me learn a thing or two about them.