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

Changing GUI Colours Randomly?

Asked by 9 years ago

So I made a script which changes the colour scheme for two GUI's when a button is pressed. It prints the random colour numbers but does not visually change the colour. Any help?

colorgui = script.Parent.ColorChange
function colorChange()
    local MessageGui = script.Parent.MessageGui
    MessageGui.BackgroundColor3 = Color3.new(math.random(300), math.random(300), math.random(300))
    print(MessageGui.BackgroundColor3) --DEBUG
    colorgui.BackgroundColor3 = MessageGui.BackgroundColor3
end

colorgui.MouseButton1Down:connect(colorChange)

2 answers

Log in to vote
0
Answered by 9 years ago

Problem 1:

Color3 values only go up to 255.

Problem 2:

You need to specify two numbers when using math.random().

E.G

math.random(1,10) -- random number between 1 and 10

Problem 3:

To avoid bugs, do this when changing Color3 values:

Color3.new(number/255,number/255,number/255)
0
Thanks! I'm not exactly great at this yet. :P Increated 25 — 9y
0
Actually, Color3 is only between 0 and 1, and the two arguments in math.random aren't needed. math.random(1, 10) is the same as math.random(10) Perci1 4988 — 9y
0
Really? Thanks! TheDeadlyPanther 2460 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Color3 takes a number between 0 and 1. If you want to use a higher number, you have to divide it by that same number to again receive a value between 0 and 1.

MessageGui.BackgroundColor3 = Color3.new(math.random(300)/300, math.random(300)/300, math.random(300)/300)

Or you can just not supply any arguments, in which case math.random will give you a number between 0 and 1;

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

Answer this question