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