The Wait() in my code won't work properly (coroutine.wrap)

I’m working on an alarm system which it makes a specific light bulb flicker by changing it’s material to Neon and Plastic rapidly with a 0.25 second gap, start playing a looped alert sound and make players be aware of there’s a problem about a specific thing, stop when the problem is solved, then start again if the problem occured once again.

I was actually doing it for each alarm with separate scripts on my older games, but I decided to give coroutines a try and do it all in a single script, I almost have no experience about coroutines and sometimes keep failing about it.

So the problem here is; everything works properly but the light bulb won’t flicker and just keeps staying at Neon when it’s supposed to flicker/blink.

while wait(0.25) do
function spawner(func)
	local wrap = coroutine.wrap(func)
	wrap()
end
spawner (function()
if workspace.Overheated.Value==true then
	script.Parent.ExtremeTempLight.Material=("Neon")
	wait(0.25)
	script.Parent.ExtremeTempLight.Material=("Plastic")
	wait(0.25)
end
end)
spawner (function()
if workspace.Overpressure.Value==true then
	script.Parent.ExtremePressureLight.Material=("Neon")
	wait(0.25)
	script.Parent.ExtremePressureLight.Material=("Plastic")
	wait(0.25)
end
end)
spawner (function()
if workspace.Overheated.Value==true or workspace.Overpressure.Value==true then
    if workspace.SoundPart.Alarm1.Playing==false then	
    workspace.SoundPart.Alarm1.Playing=true
    end
end
end)
spawner (function()
if workspace.Overheated.Value==false and workspace.Overpressure.Value==false and workspace.SoundPart.Alarm1.Playing==true then
	workspace.SoundPart.Alarm1.Playing=false
end
end)
end

The weird thing is that when I change the,

while wait(0.25) do

to;

while wait() do

in string 1 , the part (light bulb) keeps flickering at lightspeed as if the wait(0.25) between the strings (9,11,17 and 19) doesn’t exist.

How do I solve this?

1 Like

If multiple coroutines are affecting the light they will conflict with each other and each wait call is independent of all other wait calls. So, it’s likely it is waiting but on multiple coroutines and they’re firing within very close timing of one another.

1 Like

Also, couldn’t you just simply use task.spawn() instead of defining a function that creates new functions and then calling those said functions?
Not to mention, wait() will soon be deprecated in favor of the task library’s own wait function. Consider using those.

1 Like

There’s only one coroutine affecting a specific bulb, for example the one in string 6 is for a different bulb and the one in string 14 affects the another, etc. Each bulbs are unique and only for a specific alert.

Tried spawn() before using coroutines, I guess I’ll give it a try once again.

spawn() is different from task.spawn().

He’s talking about the loop. Since coroutines don’t yield the scope the function itself is in, it will run unrestricted. You have the lightbulb switch materials after 0.25 seconds, but then also have the entire script repeat every 0.25 seconds?

Maybe work

function spawner(func)
	local wrap = coroutine.wrap(func)
	wrap()
end
function neonOrPlastic(material)
	if material == Enum.Material.Plastic then
		return Enum.Material.Neon
	else
		return Enum.Material.Plastic
	end
end

while wait(0.25) do
	spawner(function()
		if workspace.Overheated.Value==true then
			script.Parent.ExtremeTempLight.Material = neonOrPlastic(script.Parent.ExtremeTempLight.Material)
		end
	end)
	spawner(function()
		if workspace.Overpressure.Value==true then
			script.Parent.ExtremePressureLight.Material = neonOrPlastic(script.Parent.ExtremePressureLight.Material)
		end
	end)
	spawner(function()
		if workspace.Overheated.Value==true or workspace.Overpressure.Value==true then
			if workspace.SoundPart.Alarm1.Playing==false then	
				workspace.SoundPart.Alarm1.Playing=true
			end
		end
	end)
	spawner(function()
		if workspace.Overheated.Value==false and workspace.Overpressure.Value==false and workspace.SoundPart.Alarm1.Playing==true then
			workspace.SoundPart.Alarm1.Playing=false
		end
	end)
end

PS: Coroutine are pretty useless in this script

1 Like

Try this:

local tempLight = script.Parent.ExtremeTempLight
local pressureLight = script.Parent.ExtremePressureLight
local overheated = workspace.Overheated
local overpressure = workspace.Overpressure
local alarm = workspace.SoundPart.Alarm1

local cycle
while task.wait(.25) do
  cycle = not cycle
  local oh = overheated.Value
  local op = overpressure.Value
  local cycleMat = cycle and 'Neon' or 'Plastic'
  tempLight.Material = oh and cycleMat or 'Plastic'
  pressureLight.Material = op and cycleMat or 'Plastic'
  alarm.Playing = (oh or op) and cycle or false
end
1 Like

The whole script won’t work if I don’t make it run in a loop such as while wait(0.25) do, the reason why I’m making it repeat every 0.25 seconds is because when I make it while wait() do or while true do it runs at lightspeed and ignores the wait(0.25) while changing the materials, thats the problem.

Try my solution first.

I know it has to loop, we’re pointing it out to you that the 0.25 seconds repeat interval is conflicting with the flashing interval inside the coroutines

1 Like

Works, but the alarm sounds a bit buggy.

I think the alarm is also in the .25 second loop in your code, that’s why It sounds like it keeps playing/stopping every .25 seconds, but will take care of it. Thanks.

remove ‘and cycle’ at line 15 but nice script

1 Like

I thought he would want the alarm to repeat like the lights which is why I included that lol

@JBhappy2 @Prototrode both of your solutions work properly and both of you posted at the same time, idk which one should I submit as a solution lol.

Also thanks to both of you, you just saved me from trying to blow my last braincell up
while trying to solve this thing with my terrible scripting skills.

Tachiyen’s solution is the best, so put it on him

2 Likes