How to repeat code until event is fired

StartBlinkingEvent.Event:Connect(function()	
	repeat
		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Really red")
			end
		end
		wait(IntervalInSeconds)
		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Medium stone grey")
			end
		end
		wait(IntervalInSeconds)
	until StopBlinkingEvent.Event	
end)

When the event “StartBlinking” is fired, the lights are suppose to blink between Red and Grey, and I want then to keep blinking untill the “StopBlinkingEvent” is fired.

until StopBlinkingEvent.Event

However, it does not work. It only plays the code once then stops. Nothing in output. How do i achieve this?

You could try using a while loop (or repeat like you done), and have a connection setup that changes the value of a variable, which can toggle the loop:

i.e.

StartBlinkingEvent.Event:Connect(function() 
    local Active = true
    
    StopBlinkingEvent.Event:Once(function()
        Active = false
    end)

    while Active do
        -- Your blinking code here
    end
end)
1 Like

what does :Once do? I never seen that before

It’s basically identical to :Connect(), except it disconnects itself after one usage. It’s mainly just used to save that tiniest bit of RAM.

Wait, its has a problem tho,

Heres the porrtion of the code first, with your modifications in it

local IntervalInSeconds = 0.4

for _,Lights in pairs(LightsTable) do
	if Lights:IsA("BasePart") then
		Lights.BrickColor = BrickColor.new("Lime green")
	end
end

local Active

StartBlinkingEvent.Event:Connect(function() 
		Active = true
	StopBlinkingEvent.Event:Once(function()
		Active = false
		
		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Lime green")
			end
		end
		
	end)

	while Active do
		
		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Really red")
			end
		end
		wait(IntervalInSeconds)
		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Medium stone grey")
			end
		end
		wait(IntervalInSeconds)
		
	end
end)




robloxapp-20241105-2128180.wmv (598.7 KB)

Why does the light blink, only to become medium stoine grey at the end, when its suppose to be green?

Because the while loop has to finish terminating before it checks if it is still active.
Try adding

if not Active then break end

to line 31, just after the wait.

1 Like

Still does not work, light still turns medium grey afterwards

Can you show the code, with the if statement added?

1 Like

local IntervalInSeconds = 0.4

for _,Lights in pairs(LightsTable) do
	if Lights:IsA("BasePart") then
		Lights.BrickColor = BrickColor.new("Lime green")
	end
end

local Active

StartBlinkingEvent.Event:Connect(function() 
	Active = true
	StopBlinkingEvent.Event:Once(function()
		Active = false

		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Lime green")
			end
		end

	end)

	while Active do

		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Really red")
			end
		end
		if not Active then break end
		wait(IntervalInSeconds)
		for _,Lights in pairs(LightsTable) do
			if Lights:IsA("BasePart") then
				Lights.BrickColor = BrickColor.new("Medium stone grey")
			end
		end
		if not Active then break end
		wait(IntervalInSeconds)

	end
end)

the if statement should go after the wait statement

1 Like

Ohh now it works. Sorry, about that, my english isnt that good. Thank you for your help :grinning:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.