Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is wrong with this flickering light?

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

✓ Accepted answer

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

2 comments

1

Works like a charm. Thanks!

1

Use math.random() instead of math.random(1,10) / 10 -- much briefer and won't be in intervals of tenths of a second

This discussion is preserved as it was. New answers, comments, and votes are closed.