Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to randomly choose a string from a table?

Hey there. I've been having a bit of trouble with a simple piece of code. I was wondering if anyone here could help. Basically, I made a table, and used a portion of code to pick out a random one of the strings within said table. It's not working the way I want it though.

local colorTable = {
		"Really red";
		"Deep orange";
		"New Yeller";
		"Forest green";
		"Lapis";
		"Teal";
		"Magenta";
		"Pink";
		"Carnation pink";
		"Really black";
}
	beam.BrickColor = BrickColor.new(math.random(#colorTable))

The 'beam' part is made out of neon material. However, I've changed this material to other things and I've still encountered the same issue.

My assumption is that I have the wrong equation in use within the BrickColor.new() parameters.

Help is greatly appreciated.

-Kon

2 answers

✓ Accepted answer

You were pretty close.

local colorTable = {
        "Really red";
        "Deep orange";
        "New Yeller";
        "Forest green";
        "Lapis";
        "Teal";
        "Magenta";
        "Pink";
        "Carnation pink";
        "Really black";
}

local function rancolor(tab)
    return BrickColor.new(tab[math.random(1,#tab)]) --a random number between 1 and the length of the table
end

beam.BrickColor = rancolor(colorTable)

This discussion is preserved as it was. New answers, comments, and votes are closed.