✓ 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())
math.random()
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)
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.
3 comments
use math.random()
Ah never thought about that.
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.