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

Am I missing something with this Color3.new?

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!

4 comments

0

Color3.New(1, 1, 1) is pure white. The numbers are max of one, min of zero. Some people make a color function that divides all values by 255. Color3.new(161/255, 161/255, 161/255) is what you really need for line 6.

0

I'll try it, but it changes the color to 0, 255, 0 when it is told to.

0

Well it worked, thanks for your help!

0

Yep, that threw my head for a few spins too before someone told me that one day. :)

1 answer

✓ Accepted answer

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

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