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

Select randomly from a table forever?

So basically this only works once to select one random color from the table, how could I may this work forever? (Something to do besides a for loop?)

local Colors = require(script.ModuleScript)
local Color2 = ""

function lerpToColor3(obj, c3)
    for i = 0, 1, 0.05 do
        obj.BackgroundColor3 = obj.BackgroundColor3:lerp(c3, i)
        wait()
    end
end


function color()
	for i,v in pairs(Colors) do
		Color2 = v -- string value (in a module script there is a table that contains all the BrickColor names I want to use)
	end
end

while true do
	wait()
	color()
	lerpToColor3(script.Parent, BrickColor.new(Color2).Color)
end

3 comments

1

use math.random()

0

Ah never thought about that.

1

Try and utilise math.randomseed(), passing tick() as the argument in attempt to make it a little more random. As long as you have the same seed, you'll get the same results.

1 answer

✓ Accepted answer

It is considered better practice not to use global variables like "Color2". Even though you've declared it local, it's still in the scope of the majority of your script, and it needn't be. Avoiding global variables increases the re-usability of functions that you write, as they won't be relying on global state -- it's also easier to prevent bugs. (It's less important with small scripts.)

To select a random value, you need math.random. Improved script:

math.randomseed(tick()) -- initialize the random generator
math.random() -- first result isn't very random; you can throw away more results if you want

local Colors = require(script.ModuleScript)

function lerpToColor3(obj, c3)
	for i = 0, 1, 0.05 do
		obj.BackgroundColor3 = obj.BackgroundColor3:lerp(c3, i)
		wait()
	end
end

function selectRandom(list)
	return list[math.random(1, #list)]
end

while true do
	wait()
	local color2 = selectRandom(Colors)
	lerpToColor3(script.Parent, BrickColor.new(color2).Color)
end

Note how selectRandom returns a value instead of assigning it to a global variable. Likewise, in the while loop, the return value is assigned to the local variable color2, which is then used in the next line. This keeps things more flexible (selectRandom doesn't need to know what you're assigning to); you could use selectRandom in multiple places with different lists and you needn't worry about it overwriting your values. ex, say you wanted 2 different colors at once:

local color1 = selectRandom(Colors)
local color2 = selectRandom(Colors)
--use both in lerp or however you want

Before, you'd have a problem because both calls would try to use the same global variable (overwriting the previous value before you were ready to use it). But since selectRandom doesn't use a global variable, you don't have to worry about that problem.

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