Search code examples
luacolorsnumbersrgbroblox

Problems with number, and Color3 values


I have been trying to make a TextLabel change color from red, to green over the RGB spectrum more each click. I did have it working, but on seperate parts the color went on the stage after the one on the last clicked part, so I tried changing out the variables for values inside the part, and now it goes light green for a while and then dark green. The red, green, and blue values are 'number values', and the color value is a 'Color3' value.

local parts = script.Parent.Parent:WaitForChild("Collectables"):GetChildren()

for i, v in pairs(parts) do
    if v:IsA("Part") then
        if v:WaitForChild("ClickDetector") then
            if v:FindFirstChild("Values") then
                local Values = v:FindFirstChild("Values")
                Values.Red.Value = 255
                Values.Green.Value = 0
                Values.Blue.Value = 0
                Values.Change.Value = true
                v.ClickDetector.MouseClick:Connect(function()
                    if v:FindFirstChild("SurfaceGui") then
                        v.SurfaceGui.TextLabel.Text = v.SurfaceGui.TextLabel.Text + 1
                        if Values.Change.Value == true then
                            if Values.Green.Value < 250 then
                                Values.Green.Value += 50
                            elseif Values.Green.Value == 250 then
                                Values.Green.Value += 5
                            elseif Values.Green.Value == 255 and Values.Red.Value > 5 then
                                Values.Red.Value -= 50
                            elseif Values.Red.Value == 5 then
                                Values.Red.Value -= 5
                                Values.Change.Value = false
                            end
                            Values.Color.Value = Color3.new(Values.Red.Value,Values.Green.Value,Values.Blue.Value)
                            v.SurfaceGui.TextLabel.TextColor3 = Values.Color.Value
                        end
                    end
                end)
            end
        end
    end
end

Solution

  • Color3.new creates a color where the RGB values are 0 to 1. So Color3.new(1,1,1) is white, Color3.new(.5,.5,.5) is medium gray, and Color3.new(0,0,0) is black.

    In your case you want the RGB to be from 0 to 255, so either divide by 255: Color3.new(125/255, 100/255, 50/255)
    Or just use Color3.fromRGB: Color3.fromRGB(50, 100, 200)