Alright, I was reading through all these replies and there might already be one that works but a lot of them are flawed for one reason or another. I’ll cover why some of them are flawed and then provide a solution:
First, the R, G, and B properties of Color3 datatypes are read-only! This means that you can only get the value of them, but you cannot assign directly what each one is. In order to reassign the Color3 property of any object you need to use one of the Color3 constructors.
Second, you might be having issues because you were using the wrong constructor. Here’s a link to the wiki for the Color3 datatype. To summarize constructors, you have 4 options:
Color3.new()
--No arguments. This will return the darkest black
Color.new(r,g,b)
--This will create a color from Red (r), Green, (g), and Blue (b) arguments.
--r, g, and b should all be in the range [0, 1]; anything outside will be clamped to this range
Color3.fromRGB(r,g,b)
--Similar to Color3.new(r,g,b), however the r, g, and b arguments should be
--within the range [0, 255]. This constructor exists essentially just for code readablility
--as this range might feel a little more natural when working with RGB colors
Color3.fromHSV(hue, saturation, value)
--This ones a bit more complex and has very niche uses that I won't cover here
Third, as far as creating a random color goes, you would want to make 3 distinct calls to math.random()
. If you only make the call to math.random()
once and provide its result to each of the arguments r,g, and b, then sure, you will get a “random” color each time, however the range of random colors you get will not cover the entire color space.
FINALLY THE CODE:
function getRandomColor()
local r = math.random() --With no arguments math.random() returns a number on the range [0,1]
local g = math.random() --Make the call again so that 'g' will be different from 'r' (or have the very rare chance of being the same)
local b = math.random() --Same explanation as 'g'
return Color3.new(r,g,b)
end
You can now just simply use this function when changing the Color of a Part
while true do
wait(0.1)
script.Parent.Color = getRandomColor()
end
Same function as getRandomColor() using Color3.fromRGB()
function getRandomColor()
local r = math.random(0, 255)
local g = math.random(0, 255)
local b = math.random(0, 255)
return Color3.fromRGB(r,g,b)
end