Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is the brightness in loop not accurate? [Solved]

Asked by 4 years ago
Edited 4 years ago
--Script that makes light flicker and fade and whatever

--Variables for objects
local lightModel = script.Parent
print(lightModel)
local lightPart = lightModel.Light
print(lightPart)
local light = lightPart.SurfaceLight
print(light)

--Variables for light configurations are not usable, in my opinion
--Found out that creating variables directly for a property, will get the number
--But not the property itself. Just takes number. So you must manually edit, as it is below

--Variables for a few amounts
local LOWEST_AMOUNT = 0.1
local HIGHEST_AMOUNT = 1

--Smoothness is like how many times it loops
local function fadeOutAndFadeIn(initialAmount, lowestAmount, smoothness, transitionSpeed)
    for currentAmount = initialAmount, lowestAmount, -smoothness do
        light.Brightness = currentAmount
        print(light.Brightness)

        wait(transitionSpeed)
    end

    for currentAmount = lowestAmount, initialAmount, smoothness do
                print(currentAmount)
        light.Brightness = currentAmount
        print(light.Brightness)

        wait(transitionSpeed)
    end
end




while true do
    fadeOutAndFadeIn(HIGHEST_AMOUNT, LOWEST_AMOUNT, 0.1, 0.1)

    wait()
end

Ok firstly just ignore stupid comments just for me to keep some info and stuff to remember. Ok so basically, not much of a problem, but it irritates me. I want to know why, when the for loop goes down and up, it has many digits; like the number is very close to the expected one, but it is not, when looping down. For example, you would expect the first loop down to be 0.9, but the number would be like 0.8999.etc and whatever. I want to understand why this happens, and perhaps how to stop this from happening, WITHOUT manually rounding the numbers. Weirdly the current amount that is printed is accurate, just the brightness. Maybe this isn't too important, but it irritates me. Thanks in advance.

0
ROBLOX is weird with this; however, it will not effect your program's performance. Ziffixture 6913 — 4y
0
Roblox sometimes uses Doubles, double-precision floating point number, https://en.wikipedia.org/wiki/Double-precision_floating-point_format. Doubles do not get exact values, but get VERY VERY close to the number that's it's supposed to reach. To fix this, all you have to do is to check if the value is >= 0.5, if it is then use math.floor to round up. killerbrenden 1537 — 4y
0
If the value is < 0.5 then use math.ceil to round down. https://developer.roblox.com/en-us/api-reference/lua-docs/math. killerbrenden 1537 — 4y
0
math.ceil() rounds up, math.floor() rounds down ^^ Ziffixture 6913 — 4y
View all comments (4 more)
0
Dang, I always mix those 2 up. killerbrenden 1537 — 4y
0
Your loop is based on wait() which is bad. wait does not ensure it runs at that fixed time it runs after the time expires (when it can) not on frame render. Swap to using the run service. User#5423 17 — 4y
0
Alternatively the tween service can handle this use case. User#5423 17 — 4y
0
I have not done much on tween service or run service, if not none... I am a noob...... Well I will leave this as solved since it does not affect anything. But I will look into what those services are. At some point, anyway... But thanks for all the advice anyway! Phyrixia 51 — 4y

Answer this question