How to make a flashing light in roblox studio

So I feel a little silly for asking, but how do I create a flickering light? More specifically how do I make it so that a brick changes in between two, three, or more point lights? Like if I wanted a flickering fire effect or some sort of crazy rave thing going on. I tried to reverse engineer free models that incorporated flickering point lights but I have yet to come to a conclusion. Some help would be mighty grateful.

Roblox Studio Tutorials - June 6, 2022

How to Make Flickering Lights

What’s up everyone, BuzzyBeth here – In this tutorial we’ll be learning how to make flickering lights at random intervals!

  • I created this very simple lamp post just composed of three different parts. It’s just two black parts and then the yellow part is going to be my light source.
  • Inside of that part i’m gonna go ahead and add a light source. You guys can use a point light, spotlight, or surface light either or is okay, but I want to use a spotlight to give that head-on effect with the light.
  • It’s not facing the right way so i’ll be changing that inside of the properties menu. Then change the brightness of it by using the slider.
  • Add a script inside of our part. We have a few variables, one which references our spot light and the second one is going to be just our part.
  • Now we’re going to create a while true do loop and the light is going to be disabled. We’re actually going to do math.random(0, 1) which means it can wait between 0 to 1 seconds. We’ll let them randomly choose how long it’s going to occur before light.enabled is equal to true.
  • Then we’re gonna do the same thing, we’re gonna do math.random for the wait time, but this time we’ll do zero to two seconds and then we’ll let the code choose the random time because we want this to be a random flicker!

Okay so in roblox i have been trying to make a flickering light but it doesn't seem work. So i have tried finding what my error in line one is but.. it doesn't seem to work and i can't seem to find it.

while true do math.randomseed(tick()) local Brightness = math.random(0.1 , 1) local Light = script.Parent local SpotLight = Light.SL local RandomPause = (math.random(0.5 , 5)) wait(RandomPause) SpotLight.Brightness = Brightness wait (RandomPause) SpotLight.Brightness = Brightness end

Help and Feedback Scripting Support

How do you make use Light.Enabled to make a light go blink AND have the option to turn off said light, Using while true do would break the rest of the script or simply not be able to turn it off and using RunService.HeartBeat would simply be too fast since it uses per frame. could there be any other ways to make a loop?, please tell me if so.

It would look like this, roughly.

local RunService = game:GetService("RunService") local blinking local function onLightToggle() blinking = not blinking while blinking do local lastTick = os.clock() repeat RunService.Heartbeat:Wait() until os.clock() - lastTick >= 3 -- every three seconds, oops one mistake made -- toggle the light here end end

Some other notes to take: The function must turn off the light immediately after it is toggled. After the while loop, disable the light.

Oops, accidentally had a thing backwards, it should be fixed with the latest version.

Operatik:

local blinking

Weird, I thought this line would give me a warning if I don’t assign anything…

Not quite, it is automatically nil by default.

Huh weird, it did give me a warning saying I must assign something to it. Instead of that, I had to do like this:

local blinking = nil

What exactly is that warning though? An alternative would be setting it to false though.

I’m not sure, I forgot what it is but it said not a global variable…

you should use a bool value which can handle a coroutine within a while loop : so your code won’t block

boolValue:GetPropertyChangedSignal("Value"):Connect(function() local success , lighterror = coroutine.wrap(function() while wait(delay) and boolValue.Value do light.Enabled = not light.Enabled end coroutine.yield() end)() --error handler if lighterror then error(lighterror) end --some other actions ... end)

more infos about coroutines : coroutine

Just to clear confusing a bit

Operatik:

-- toggle the light here

does this like turn on the light and does it have it turn off too?

You can do Light.Enabled = not Light.Enabled. The script would look like this otherwise:

local RunService = game:GetService("RunService") local blinking local function onLightToggle() blinking = not blinking while blinking do local lastTick = os.clock() repeat RunService.Heartbeat:Wait() until os.clock() - lastTick >= 3 -- every three seconds Light.Enabled = not Light.Enabled end Light.Enabled = false end

Additionally, I think the repeat statement needs an update from:
os.clock() - lastTick >= 3 …to…

os.clock() - lastTick >= 3 or not blinking

@Operatik it aint really doing anything for me, heres my full script btw

script.Keybind.OnServerEvent:Connect(function(Player, KeyCode) if KeyCode == Enum.KeyCode.E then for _, v in pairs(Rightlights) do light = not light while light do local lasttick = os.clock() repeat RunService.Heartbeat:Wait() until os.clock() - lasttick >= 1 v.instance.PL.Enabled = not v.instance.PL.Enabled end end end end)

yeah it uses keybinds to turn it on and off

I think that’s because the thread is not going through every light at all. It keeps looping on one light only. coroutine is advised. Also do not toggle light after the for loop, or else it will flick back to its original boolean.

use coroutines insted

script.Keybind.OnServerEvent:Connect(function(Player, KeyCode) if KeyCode == Enum.KeyCode.E then boolValue.Value= not boolValue.Value end end) boolValue:GetPropertyChangedSignal("Value"):Connect(function() local success , lighterror = coroutine.wrap(function() while wait(3) and boolValue.Value do for _, v in pairs(Rightlights) do v.instance.PL.Enabled = not v.instance.PL.Enabled end end coroutine.yield() end)() --error handler if lighterror then error(lighterror) end --some other actions ... end)

me code only uses one light so I don’t really need a coroutine yet.

Operatik:

Also do not toggle light after the for loop, or else it will flick back to its original boolean.

i absolutely am confused on this part lol.

The for loop is ran several times:

i = 1 => false i = 2 => true i = 3 => false ... i = n => n % 2 == 1

This code below might fix it, but then you have to toggle off all the lights after the next event.

local RunService = game:GetService("RunService") local light script.Keybind.OnServerEvent:Connect(function(player, keyCode) if keyCode == Enum.KeyCode.E then light = not light for _, v in pairs(Rightlights) do coroutine.wrap(function() while light do local lasttick = os.clock() repeat RunService.Heartbeat:Wait() until os.clock() - lasttick >= 1 v.instance.PL.Enabled = not v.instance.PL.Enabled end end)() end end end)

doesn’t matter how many parts you are using. Pls check coroutine’s API docs coroutine (roblox.com)

@Operatik the script worked, I found a slight error in my dictionary which does not update Rightlights
guess this is how to use Heartbeats.

@polloarrosto01 sorry i must have misinterpret “tasks” as part functions.

3 Likes

Postingan terbaru

LIHAT SEMUA