Help making a color random

im trying to make a game but i cant get a brick to have a random color every tenth of a second
the code is

while wait(0.1) do
local a = math.random(1,255)
local b = math.random(1,255)
local c = math.random(1,255)
script.Parent.Color.B = a
script.Parent.Color.R = b
script.Parent.Color.G = c
end

Maybe you can try using math.randomseed(os.time()) ?

while wait(0.1) do
	local a = math.random(1, 255)

	script.Parent.Color = Color3.fromRGB(a, a, a)

end

Hope this helps

2 Likes

you need to change the color of a part using color3, not change the color of the parts 3 times. Use this instead

while wait(0.1) do
	local r = math.random(0,255)
	local g = math.random(0,255)
	local b = math.random(0,255)
	script.Parent.Color = Color3.fromRGB(r, g, b)
end
14 Likes

that just picks a random color from white to black

Lol we made the same thing just mine only has one math.random(1, 255) line but put in fromRGB() 3 times

while wait(0.1) do
    script.Parent.BrickColor = BrickColor.random()
end
5 Likes
local part = script.Parent

while true do
    wait(0.1)
    part.Color = Color3.fromRGB(math.random(0,255), math.random(0,255), math.random(0,255))
end
2 Likes

that is fine, however it has less colors to pick from.

This is almost the same with mine, this is better but mine is more understandable.

2 Likes

Here is my approach:

local function randomRGB()
    -- you can put a seed inside Random.new( ) if you want
    return Color3.fromRGB(
        Random.new():NextInteger(0, 255),
        Random.new():NextInteger(0, 255),
        Random.new():NextInteger(0, 255)
    )
end

while true do
    wait(0.1)
    script.Parent.Color = randomRGB()
end

Color3 value has more than one constructor by the way, there is .fromRGB, .fromHSV, and .new.

You can customize your randomization of the colors with different constructors or using different methods to picking values for your color constructor arguments.

Random.new() is more recommended than math.random() in my opinion because it’s easier to get around using. You can still use math.random() if you’d like.

I would recommend you doing
local a = math.random(1,255)
local b = math.random(1,255)
local c = math.random(1,255)
while wait(0.1) do
script.Parent.Color.B = a
script.Parent.Color.R = b
script.Parent.Color.G = c
end

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
3 Likes

Try using BrickColor.Random, probably something like this:

while wait(0.1) do
    script.Parent.BrickColor = BrickColor.Random
end
1 Like