How would I make a Color3 value that slowly gets darker, based on an intvalue

What I want is that a color3 value to get darker based on an intvalue.

--For example:
--The intvalue slowly goes down to 0 with a loop. It starts at 10.
--If the intvalue is at 5, the color3 would be 50% darker.
--If it's at 10, the color3 value will be its base color.

None of my attempts have worked, I would be grateful if someone could help me

1 Like

You can change the shade of each part of the Color3 value by multiplying it by a fraction. The smaller the fraction, the darker the shade.

1 Like

I tried this and the dev console said β€˜R cannot be assigned to’

Send the code you used, worked fine for me when I was experimenting

script.Parent.Battery.Value = script.Parent.Battery.Value - 0.1
local percentage = script.Parent.Battery.Value / 20 -- battery is 10

bar.BackgroundColor3.R = Color3.fromRGB(32, 236, 144).R * percentage 

bar.BackgroundColor3.G = Color3.fromRGB(32, 236, 144).G * percentage

bar.BackgroundColor3.B = Color3.fromRGB(32, 236, 144).B * percentage

As the error says, you cannot write it like that.
Instead, something like:
Color3.fromRGB(20 * (1/2), 43 * (1/2) ... etc should work fine

In the case where you want to only change the brightness, you might want to use Color3:ToHSV and Color3.fromHSV to do this. It will retain hue and saturation while only changing value/brightness.

local intValue = workspace.IntValue
local color3Value = workspace.Color3Value
local hue, saturation = color3Value.Value:ToHSV()

intValue:GetPropertyChangedSignal:Connect(function()
    local newValue = intValue.Value / 10 -- in this case, "value" refers to the value (brightness) of the colour
    color3Value.Value = Color3.fromHSV(hue, saturation, newValue)
end)
6 Likes

you should use lerping for this. color3Value.Value = Color3.fromRGB(1,1,1):lerp(Color3.new(), newValue.Value / 10) --replace the 1,1,1 with your color

1 Like