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

Issue with RunService in my fading and flickering light? [Solved I think]

Asked by 4 years ago
Edited 4 years ago

Ok people so I am new to using RunService so I tried it in my light script. The script is below.

--Script that makes light flicker and fade and whatever

--Services
local RunService = game:GetService("RunService")

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

--Ignore this just some notes.
--Variables for light configurations are not usable
--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
local minFades = 1
local maxFades = 5
local minFlickers = 1
local maxFlickers = 4
local backupAmount = 0.1 
local goingDown = true
local goingUp = false
local startNewFade = false

local function fadeOutAndFadeIn(initialAmount, lowestAmount, changeAmount)

    --Light is always VERY near wanted amount, but not exactly, so if you check if it is 0 all the time, it will not be, so it will not work.
    local brightness = 1
    local finishedFade = false

    RunService.Stepped:Connect(function()
        if goingDown == true then
            print(brightness - changeAmount)
            brightness = brightness - changeAmount
            print(brightness)
            light.Brightness = brightness
            print(light.Brightness)
            if brightness <= lowestAmount then
                goingDown = false
                goingUp = true
                brightness = lowestAmount
            end
        elseif goingUp == true then
            print(brightness + changeAmount)
            brightness = brightness + changeAmount
            print(brightness)
            light.Brightness = brightness
            print(light.Brightness)
            if brightness >= initialAmount then
                brightness = initialAmount
                goingUp = false
                finishedFade = true
            end
        elseif finishedFade == true then
            brightness = initialAmount
            finishedFade = false
            goingDown = true
            startNewFade = true
        end
    end)
end

local function flickerLight(topAmount, lowestAmount, gap)
    print("Started flicker")
    light.Brightness = topAmount
    wait(gap)
    light.Brightness = lowestAmount
    wait(gap)
    light.Brightness = topAmount
    return "Done"
end


while true do
    local howManyFades = math.random(minFades, maxFades)
    local howManyFlickers = math.random(minFlickers, maxFlickers)
    print(howManyFades, howManyFlickers)

    for count = 1, howManyFades do
        fadeOutAndFadeIn(HIGHEST_AMOUNT, LOWEST_AMOUNT, 0.01)
        print("Fadehappen")
        repeat
            wait()
        until startNewFade == true
        startNewFade = false
    end
    wait()
    for flickerCount = 1, howManyFlickers do
        local flickerFunction = flickerLight()
        repeat
            wait()
        until flickerFunction == "Done"
    end
end

So what is meant to happen is it smoothly flickers a random amount of times, and then it flickers, and then that happens forever. The fading is my first issue. At first, it fades in and out smoothly, as expected. However, as time goes on, the numbers seem to jump. It is not a problem with the brightness not working, but the numbers themselves.

print(brightness - changeAmount)
            brightness = brightness - changeAmount

They gradually start to change in larger amounts. For example, 0.53 turning into 0.42, and even worse, they jump up in the process of going down. At some point they start to act like the flicker, the light value changing in values not too far from 1 and it gets worse. The prints get fast and unsteady, the lag increases. Secondly, the flickers happen randomly in the process of fading too. Maybe I should have used a different RunService thing like Heartbeat which I haven't used and barely know the difference about. Don't blame me if there is a really stupid error I am not good at this stuff. But I would like to know why this happens. It might be because of things running at the same time or whatever but please help.

Note: I will try TweenService with this too, don't say, because I haven't used that before too.

Just ask if you need more information on the results of prints. Thanks in advance.

Ahh I was told to use TweenService so yeah. Maybe the RunService is not good for this... Oh well.

Answer this question