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

How do i make a flickering light? (please send the script for it)

im trying to make a spooky game and i need a flickering light. i want the light to flicker at random times

2 answers

You can use a loop and a wait with random duration, like this:

while true do
	wait(math.random(1,2))
	part.BrickColor = BrickColor.new("Institutional white")
	light.Enabled = true
	wait(0.1)
	part.BrickColor = BrickColor.new("Medium stone grey")
	light.Enabled = false
end

where "light" is the light source and "part" is the light part, if that makes sense. This will make both the light source and the part flicker for 0.1 seconds at random intervals of between 1 and 2 seconds. You can change the numbers to adjust the timing.

A Little code I whipped up. Might need further testing, but this code you can configure when it flashes and how long the intervals between the flashes. I've also added a PointLight You may need to change this depending on what light source you're using!.

Server Script!!: Heres the code!:

local Light = script.Parent -- Our Light
local PointLight = Light:FindFirstChild("PointLight",10) -- the PointLight Object, You may need to change this!

local FlickerInt = 3 -- seconds this light flickers  
local OffFlickerInt = 10 -- How much time to make this light not flicker in seconds!
local FlickerAtStart = true -- Do we flicker at the start of the game or not?

local FlickerRandomness = true -- is our flicker point random or not?
local MaxRandomTimeBetweenFlickers = 5
local MinRandomTimeBetweenFlickers = 1 

--// Below this line is internal variables these are set as this script is run!
--// ==========================================================================

local bisFlickering = false
local t = 0
local StartTime = tick()  -- Get the time this game started at in mili-secs
local LightMode = 1
local Saved = {
	PointLight = (function()
		if(PointLight ~= nil) then
			return {
				Enabled = PointLight.Enabled,
				Range = PointLight.Range,
				Color = PointLight.Color,
				Brightness = PointLight.Brightness
			}
		else
			return nil
		end
	end)(),
	FlickerInt = FlickerInt,
	OffFlickerInt = OffFlickerInt,
	FlickerAtStart = FlickerAtStart	,
	FlickerRandomness = FlickerRandomness,
	MaxRandomTimeBetweenFlickers = MaxRandomTimeBetweenFlickers,
	MinRandomTimeBetweenFlickers = MinRandomTimeBetweenFlickers
}
--// ==========================================================================
--// end of internal varibles


--// Main Loopy!
while true do
	t = tick() -- Grab Game Tick(frame) time!...
	local DT = t - StartTime -- Subtract the tick time from StartTime, this gives us the seconds since last update. Simular to workspace.DistributedGameTime but more accurate!
	if(FlickerAtStart == true) then -- do we flicker at start?
		
		if(DT <= FlickerInt) then -- Time is still under Flicker time!
			bisFlickering = true
		else -- Right we're Done flikering lets rest and reset the StartTime for next time!...
			StartTime = tick()
			bisFlickering = false
			FlickerAtStart = false
		end
	end
	if(bisFlickering == true) then-- Main Flicker if Statement, are we flickering?
		if(DT <= FlickerInt) then -- if DT is les then our Flicker time then randomise the LightIntensitiy via LightMode
			LightMode = math.random()
		else -- We're Not Flikering lets reset everything
			LightMode = 1
			bisFlickering = false
			if(FlickerRandomness == true) then
				OffFlickerInt = math.random(MinRandomTimeBetweenFlickers,MaxRandomTimeBetweenFlickers)
			else
				OffFlickerInt = Saved.OffFlickerInt
			end
		end
	else -- Nope Not Flikering Lets rest....
		LightMode = 1
		if(DT<= OffFlickerInt) then -- Snooore.......*PFFFT*..... Snoore.....
			bisFlickering = false
		else -- Wake up we're Flickering!....
			StartTime = tick()
			bisFlickering = true
		end
	end

--// Talk to our light Brick and if available our PointLight
	Light.Color = Color3.fromHSV(0, 0, LightMode)
	if(PointLight ~= nil) then
		PointLight.Range = (LightMode*100 / Saved.PointLight.Range) * 10
	end
	
	-- allways waiting.... No seriously, removing this will hang the game!
	wait()
end

Hopefully i've added some useful comments. If not, let me know. Oh! and simply put this script inside the light you want to flicker randomly. Also if you want it to talk to your Light Object just change line 2 to the name of your Light Object.

Hope this helps! :)

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