How would I make everything in a folder happen at once?

I’m currently making a fire alarm system for a school I’m working on but I’m having trouble because when you click the ClickDetector it only plays for one of the alarms and not the rest.

strobe = game.Workspace.FireAlarm.Strobe
strobeimage = strobe.Flasher.Strobe
horn = game.Workspace.FireAlarm.horn.Sound
strobelight = strobe.Light
local clickDetector = script.Parent.ClickDetector

function onMouseClick()
	horn.Looped = true
	horn.Playing = true
	while true do
		wait(1)
		strobeimage.Visible = true
		strobelight.Enabled = true
		wait(0.1)
		strobelight.Enabled = false
		strobeimage.Visible = false
	end
	
end
	
clickDetector.MouseClick:connect(onMouseClick)

Folder name is "Alarms"

2 Likes

You could use a for loop to get each element of the array (Folder)

Well I was thinking it could Get the children of the folder. Storbe, StrobeImage, Horn, and Strobelight…

In my case i’d just loop throught every item in the folder , then enable it and disable it

If it isnt working I’d loop again but create a separated thread for each alarms so it could loop without impacting the others

How exactly would I achieve this, I’m having a bit of a struggle understanding…?

Your variable only gets one horn:
horn = game.Workspace.FireAlarm.horn.Sound

Instead, use :GetChildren() to get all children of FireAlarm (I assume this is where your multiple horns exist.):
horn = game.Workspace.FireAlarm:GetChildren()

After that, we have to do a for loop to go thru every children of the FireAlarm instance and play their audio:

for i, v in pairs(horn) do
     v.Sound.Playing = true
end

The v will be the children inside FireAlarm as we referenced horn as FireAlarm:GetChldren(), basically it get’s every single child of it, and we used v to return them

Let me know if this goes wrong and i’ll try to help. :grinning_face_with_smiling_eyes:

Its just a template.

for _, v in pairs(Folder:GetChildren()) do
	if v:IsA("Sound") then -- replace this with how you cound check if its an alarm e.g if v.Name == "" then ...
		spawn(function() -- Spawns a new thread
			while true do
				v.Enabled = true -- loop
				-- rest of code
			end
		end)
	end
end

To make it simpler, I edited your script to fully work if you didn’t understand what to do:

strobe = game.Workspace.FireAlarm.Strobe
strobeimage = strobe.Flasher.Strobe
horn = game.Workspace.FireAlarm:GetChildren()
strobelight = strobe.Light
local clickDetector = script.Parent.ClickDetector

function onMouseClick()
	for i, v in pairs(horn) do
	   	if v.Name == horn then
         		v.Sound.Playing = true
			v.Sound.Looped = true
		end
	end
	while true do
		wait(1)
		strobeimage.Visible = true
		strobelight.Enabled = true
		wait(0.1)
		strobelight.Enabled = false
		strobeimage.Visible = false
	end
	
end
	
clickDetector.MouseClick:connect(onMouseClick)

It still seems to only be playing for one alarm still and now the sound isn’t playing…?

Can you show the hierachy of the alarms?

image

Oh… my bad, I thought every horn is in FireAlarm, okay now try this:

strobe = game.Workspace.FireAlarm.Strobe
strobeimage = strobe.Flasher.Strobe
horn = game.Workspace.Alarms:GetChildren()
strobelight = strobe.Light
local clickDetector = script.Parent.ClickDetector

function onMouseClick()
	for i, v in pairs(horn) do
               if v.Name == "horn" then
         		v.Sound.Looped= true
        		v.Sound.Playing = true
		end
	end
	while true do
		wait(1)
		strobeimage.Visible = true
		strobelight.Enabled = true
		wait(0.1)
		strobelight.Enabled = false
		strobeimage.Visible = false
	end
	
end
	
clickDetector.MouseClick:connect(onMouseClick)
1 Like

OOPS. Lemme edit this, found an infinite yield. Edit: done

1 Like

There is one tiny issue I think I can see, but unsure how to fix.

The strobe = game.Workspace.FireAlarm.Strobe is making the script not working since It’s getting it for only one alarm and not all of them if I’m correct?

Found the issue but the flashing only plays for one alarm and not the others and the sound is still not playing?

for i,v in pairs(script.Parent:GetChildren()) do
     if v:IsA('BasePart') then
          v.CanCollide = true
     end
end

(Example Above)

I was thinking if it would be possible to do something like this maybe, but I’m unsure?

What needs to be modified for every FireAlarm model?

Are you trying to enable a horn and light effect found in each alarm?

Yes, and the strobe image as well…

local IsPlayingAlarms = false

function onMouseClick()
    if (IsPlayingAlarms) then return end -- Check if alarms are already playing.

    for i, alarm in pairs(Alarms:GetChildren()) do
        coroutine.resume(coroutine.create(function() -- Thread the effects.
            alarm.horn.Sound:Play() -- Sound the alarm.

            while (IsPlayingAlarms) -- Strobe effect until IsPlayingAlarms = false
               wait(1)
	           strobeimage.Visible = true
	           strobelight.Enabled = true
	           wait(0.1)
	           strobelight.Enabled = false
	           strobeimage.Visible = false
            end

            -- Reset all alarm effects after IsPlayingAlarms = false
            alarm.horn.Sound:Stop()
            strobelight.Enabled = false
	        strobeimage.Visible = false
        end))
    end
end

Basically, when the button is clicked, you trigger a boolean value. That value determines if the alarm effects are in play or not. Then, all the alarm effects are threaded so you don’t hang the main thread in case you want to process more code after all effects have been activated.

To stop the effects, simply do IsPlayingAlarms = false wherever you feel is most convenient. That should stop and reset all alarm states.

while (IsPlayingAlarms) -- Strobe effect until IsPlayingAlarms = false
wait(1)

This shows as an error since it thinks it a loop??

Did you make sure to add IsPlayingAlarms = false before the OnClick function?