Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
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)

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. User#11440 120 — 7y

2 answers

Log in to vote
0
Answered by
DevChris 235 Moderation Voter
7 years ago
Edited 7 years ago

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
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. ScriptGuider 5640 — 7y
0
You are correct DevChris 235 — 7y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

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())

Answer this question