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

Why does math.random give me huge number when i set its limit to 255?

local button=script.Parent
local frame=button.Parent

button.MouseButton1Click:connect(function()
print("clicked")
frame.BackgroundColor3=Color3.new(math.random(0, 255),math.random(0, 255),math.random(0, 255))
end)

1 comment

2

When using Color3, you have to divide your number by 255. So math.random(1,255)/255. Alternatively, you could just use math.random() and it will give you random colors.

2 answers

✓ Accepted answer

Color3.new() ranges from 0 to 1. You have to divide the R, G and B by 255 or use the range 0 to 1;

game.StarterGui.ScreenGui.Frame.BackgroundColor3 = Color3.new( math.random(), math.random(), math.random() )

2 comments

2

While this is correct, I'd like to just add that passing an argument to this function is redundant for this specific situation, since math.random() alone will return a number ranging from 0 to 1.

0

You are correct

The parameters to Color3.new are actually between 0 and 1, not 0 and 255 (as you might guess from the Properties tab).

As a result, you should use numbers from 0 to 1 instead of 0 to 255:

frame.BackgroundColor3 =
	Color3.new(math.random(), math.random(), math.random())

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