From Odoth's sample code I switched out wait()
with tick()
, changed variable names to match my coding style, and broke up the formula onto two lines for readability.
It's working well, the only issue is it doesn't stop perfectly at FadedBrightness (Goes past to about .298), so I've included a line at the bottom of the method to counteract the inaccuracy.
Timing works perfectly and I'm very happy with the result.
Completed method:
function LightSource:DecreaseBrightness()
local light = self.Light
local startBrightness = light.Brightness
local startTime = tick()
local endTime = startTime + self.DarkenSpeed
while light.Brightness >= self.FadedBrightness do
local currentTime = tick()
local timer = (currentTime - startTime) / (endTime - startTime)
local brightness = startBrightness - (startBrightness - self.FadedBrightness) * timer
light.Brightness = brightness
wait()
end
-- Set the brightness in case the loop spills over.
light.Brightness = self.FadedBrightness
end