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

What is wrong with this flickering light?

Asked by 9 years ago

This script is supposed to make a SpotLight turn on and off, but I haven't gotten any good results. The light just stays on indefinitely. This script is in a part called "Light1", and there is a SpotLight inside Light1.

local light = script.Parent.SpotLight
local time1 = math.random(1, 5)
local time2 = math.random(1, 5)

function flicker()
    while true do
        light.Enabled = true
        wait(time1)
        light.Enabled = false
        wait(time2)
    end
end

I don't know what I did wrong and the script checker thingy didn't say there were any exceptions.

1 answer

Log in to vote
2
Answered by
Vrakos 109
9 years ago

If you're looking for more realistic results, speed up the flicker and refrain from making math.random a variable as it will stay at that number and wont change. I've re-written the script for you.

local Light = script.Parent.SpotLight

while true do
    Light.Enabled = not Light.Enabled
    wait(math.random())
    Light.Enabled = not Light.Enabled
    wait(math.random())
end
1
Works like a charm. Thanks! imperialstar 110 — 9y
1
Use `math.random()` instead of `math.random(1,10) / 10` -- much briefer and won't be in intervals of tenths of a second BlueTaslem 18071 — 9y
Ad

Answer this question