Simple Blinking Light

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.

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 | Roblox Creator Documentation

Just to clear confusing a bit

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

@anon81993163 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.

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)

@anon81993163 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