Pausing coroutines from other scripts?

Hey everyone, I have another question

So basically I have a light that flashes on and off, shown here

local F_L = coroutine.create(function()
	while wait(.4) do
		script.Parent.Alarm_Trigger.Alarm.Strobe.Flash.Enabled = true
		wait(.3)
		script.Parent.Alarm_Trigger.Alarm.Strobe.Flash.Enabled = false
		wait(.3)
	end
end)

Basically this is so I can control the flash below:

script.Parent.Alarm_Trigger.Interactive.ClickDetector.MouseClick:Connect(function()

	s_5:Play()
	coroutine.resume(F_L)

However, I want to try and turn the Flash Enabled property off here:

	script.Parent.Parent.Alarm_Trigger.Alarm.Strobe.Flash.Enabled = false

But I can’t because it’s in a coroutine. I’ve tried bool values, brightness, destroying the flash and re adding but nothing is working.

The goal is to have the flash turn off when the alarm is turned off and have the flash turn on when the alarm is on (so like pause the coroutine in the other script when the alarm is off and then turn it back on when its on)

Any ideas?

have you tried disabling the script when the alarm needs to go off?
like a bool value AlarmOn

when is On the script will enable the flash loop
when is Off the script will disable it

(i mean 2 script one for loop and one for check)

Just make a bindable event. When you want the flash to turn off/on, just fire the bindable event (from any script), and within the script that contains the coroutine, create a function connected to the bindable event that will pause/resume the coroutine upon the BindableEvent being fired.

2 Likes

hm yeah I suppose I could try that and see how it works, I’d just need some way to enable the script again when it’s clicked :laughing:

Alright I’ll give that one a shot, haven’t thought of that yet.

on click detector connection to 1 mouse button

if IsOn == false then
make it play and enable the loop script
IsOn = true
else
IsOn = false
make the loop script disabled

guess could work

Alright so I gave that a crack and got no luck with it, I’ve never used bindable events so I’m a bit of a beginner to them, I read up on the document about them, here’s what I got

Separate script:

local r_s = game.ReplicatedStorage

local F_L = coroutine.create(function()
	while wait(.4) do
		script.Parent.Alarm_Trigger.Alarm.Strobe.Flash.Enabled = true
		wait(.3)
		script.Parent.Alarm_Trigger.Alarm.Strobe.Flash.Enabled = false
		wait(.3)
	end
end)


r_s.AlarmFlash:Fire(F_L)

Attempt to stop in other script:

	game.ReplicatedStorage.AlarmFlash.Event:Connect(function(F_L)
		coroutine.yield(F_L)
	end)
	

No errors, didn’t work

I know I’m doing it wrong, just not sure where I’m going wrong

The event connection must be in the same script where you created the coroutine, but you can fire the event in any script.

Hm, well then that makes this pretty much useless for what I’m doing.

What do you mean? You just have to move the event handler inside the same script that the coroutine is created in, but you can still fire the event from any other script.

1 Like