Hello. I am currently working on a GUI project. My issue is, when I click the button to the "Off" position, the BackgroundColor3 does not change to 161, 161, 161
.
Here is the code:
local Button = script.Parent script.Parent.MouseButton1Click:connect(function() if script.On.Value == true then Button:TweenPosition(UDim2.new(0.5, 0, 0, 0), "Out", "Quad", 1) wait(0.5) Button.BackgroundColor3 = Color3.new(161, 161, 161) Button.Text = "Off" script.On.Value = false elseif script.On.Value == false then Button:TweenPosition(UDim2.new(0, 0, 0, 0), "Out", "Quad", 1) wait(0.5) Button.BackgroundColor3 = Color3.new(0, 255, 0) Button.Text = "On" script.On.Value = true end end)
This Script is directly inside of a TextButton. The Value is a BoolValue directly inside of the Script.
This is a Server Script (In other words, Script)
What happens is when I click it to turn Off the BackgroundColor3 is changed to 30090, 30090, 30090
instead of 161, 161, 161
.
I'm not sure if I am missing something in my script, or if Color3.new is glitched somehow. It turns 0, 255, 0
(Lime green) when turned On every time.
Thanks for your help guys! I appreciate it!
The issue is in Color3.new()
, the parameters aren't integers
. Rather, they are more like what we call double
or float
values, and they are in the range of 0 - 1. So, instead of 255, you could more properly just put 1 (though, 255 is more readable). Here's an example:
--This is how it would make more sense Color3.new(20, 255, 20) -- Should make a green color, but it would actually make white. --But this is how it actually works Color3.new(20/255, 255/255, 20/255) --This makes a green!
So, the best way to do this, while keeping everything readable, is just to use colorValue / 255
(since 255 is the maximum value, and 0 divided by anything is still 0).