Random color code, not working for lights

local function randomColor()
	return Color3.new(math.random(), math.random(), math.random())
end

Hi this code works in block but not work for light.

2 Likes

Can you show me where you set the color of the light?

1 Like
local light1_1_light = workspace.Lights.Light1_1.Light.LightPart.PointLight
local light1_1_part1 = workspace.Lights.Light1_1.Light.LightPart


			light1_1_light.Color = newColor -- not work
			light1_1_part1.Color = newColor -- work

I’m not sure if this will work, but I don’t see why it doesn’t already.

Try changing the line in the function to this

return Color3.new(math.random(0, 1), math.random(0, 1), math.random(0, 1))

Color3.new takes a number anywhere from 0 to 1. It will work with a greater number, but can cause issues.

1 Like

Oh, it works in the code in the topic, but i dont see because I set the light brightness to low ._.

2 Likes

Those two look similar but have different behavior:

print(math.random()) --a real number between 0 and 1, example 0.62
print(math.random(0, 1)) --either 0 or 1
1 Like

You are correct. Sorry lol.

Changing 1 to 0.99 should work, but I’m not entirely sure. I don’t use decimals with math.random often :laughing:

Color3.new returns from RGB, and it doesn’t range from 0 to 255. In order to make it set random colors, you must use this code snippet

math.random(0, 1) * 0.1

As it has a decimal when it comes to color3 setup, it can set any color you want.
Here’s a full-code.

local function randomColor()
	local r = math.random(0, 1) * 0.1
        local g = math.random(0, 1) * 0.1
        local b = math.random(0, 1) * 0.1
	return Color3.new(r, g, b) -- Changes to any color
end

Hope this works out well. (excuse my forum code format)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.