Issue with color3.FromHSV

I’m trying to make a flickering light where the color gets dimmer when it flickers off, but I’m having trouble with it. I’m using HSV since that’s the easiest way to make it become randomly dimmer, but instead of getting dimmer, it becomes a completely different color like red or pink. Here’s my script:

local value

while true do
	wait(math.random(0.2,2))
	
	value = math.random(25,60)

	script.Parent.Light.Color = Color3.fromHSV(47,56, value)
	script.Parent.Bulb.PointLight.Brightness = (value - 25)/ 17.5

	wait(math.random(0.2,2))

	script.Parent.Light.BrickColor = BrickColor.new "Daisy orange"
	script.Parent.Bulb.PointLight.Brightness = "3"
end

How do I fix this?

I’m not sure to why you are changing its color, but I would tween its brightness, since this is the easiest and cleanest way of doing this.

1 Like

I believe you would have to use a number between 0 - 1 since it’s not the same as RGB in the sense that you can go to 255 for each value. You can test this by doing Color.fromHSV and picking a color and it would be something like 0.4235325, 0.124124, 0.5634 so I would do math.random(100)/100 to get the value.

3 Likes

Actually you can just use math.random() with nothing in the parentheses.
Also don’t forget to change 47 to 0.47 and the 56 to 0.56.

3 Likes

Just a heads-up. I recommend using task.wait instead of wait since it will be deprecated in the future.

Also, the solution to your problem like @TheDCraft said could be just dividing by 255.

value = math.random(25,60)/255
Color3.fromHSV(47/255,56/255, value)
2 Likes

Oh, you’re right. I guess I don’t need to change the color. Also, how would you recommend tweening it?

I wrote these two scripts:

Tweening:

local _light = workspace.LightPart.PointLight
local _flickerTween = nil

local _lightMaxBrightness = 10
local _lightMinBrightness = 0

while true do
	local goalBrightness = 0
	local goalTime = 0
	if (_light.Brightness ~= _lightMaxBrightness) then
		goalBrightness = _lightMaxBrightness
		goalTime = math.random(5, 10) 
		goalTime = goalTime/10
	else
		goalBrightness =  math.random(_lightMinBrightness, _lightMaxBrightness - 3)
		goalTime = math.random(2, 7)
		goalTime = goalTime/10
	end
	
	if (_flickerTween) then
		_flickerTween:Cancel()
		_flickerTween = nil
	end
	
	local flickerTweenInfo = TweenInfo.new(goalTime)
	_flickerTween = _tweenService:Create(_light, flickerTweenInfo, {Brightness = goalBrightness})
	_flickerTween:Play()
	
	_flickerTween.Completed:Wait()
end

If you want something a little less smooth:

local _tweenService = game:GetService("TweenService")

local _light = workspace.LightPart.PointLight
local _flickerTween = nil

local _lightMaxBrightness = 10
local _lightMinBrightness = 0

while true do
	if (_light.Brightness ~= _lightMaxBrightness) then
		_light.Brightness = _lightMaxBrightness
		task.wait((math.random(5, 20))/10)
	else
		_light.Brightness = math.random(_lightMinBrightness, _lightMaxBrightness - 3)
		task.wait((math.random(2, 6))/10)
	end
end