Making a button do "multiple tasks" at once

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’d like to create a system to where when you press a button multiple alarms come on at once, I’d like to keep the script to where if I added more alarms the script would function without me having to put in a new variable and set it up.
  2. What is the issue? Include screenshots / videos if possible!
    I’ve tried to look far and wide, and I’m stumped. The best I’ve got to is having one siren sound and then the next siren sound.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Multiple variations of scripts, looking on the Dev Forms, and trying to find something in the toolbox.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- My Lua Code


local isSirenOn = false
local clickDetector = script.Parent.Parent.Buttons.Test.ClickDetector

local sirens = script.Parent.Parent.Sirens:GetChildren()
--
function SirenButtonClicked()
	if isSirenOn then
			
				for i=1,#sirens do -- Alarm sector
				if 	sirens[i].Name == "Whelen2905" then
				isSirenOn = false
				sirens[i].Sound.Sound.FedSig.ScriptOn.Enabled = false
				sirens[i].Sound.Sound.FedSig.ScriptOff.Enabled = true
				
	end
end

		else


				for i=1,#sirens do -- Alarm sector
				if 	sirens[i].Name == "Whelen2905" then
				isSirenOn = true
				sirens[i].Sound.Sound.whelen_dtmf.Playing = true
				wait(1.019)
				sirens[i].Sound.Sound.whelen_dtmf.Playing = false
				sirens[i].Sound.Sound.T4.Playing = true
				wait(16.425)
				sirens[i].Sound.Sound.T4.Playing = false
				sirens[i].Sound.Sound.FedSig.ScriptOn.Enabled = true
				sirens[i].Sound.Sound.FedSig.ScriptOff.Enabled = false
			

			end
		end
	end
end

clickDetector.MouseClick:Connect(SirenButtonClicked)


This code is able to start one siren and then start the other, I took part of this from a free model on the toolbox, and when testing that all of the lights and alarms came on at the same time, yet when trying to achieve the same-thing I get a different output.

It is possible to connect an event to a function multiple times, but there is a limit, and it’s not recommended to do so.

What I recommend is looping through over all the lights and alarms in one function instead.

To have things running in parallel, the two most common ways are using coroutines and spawn(). I’m most familiar with spawn, where you just do:

for i = 1, #sirens do
    task.spawn(function()
        -- CODE GOES HERE
    end)
end

When done in a for loop, it makes it appear as if every loop iteration is happening at the same time.

2 Likes

I never knew this actually, I’m not familiar with task.spawn at all, but thank you for this code, I’ll try it out now!

It 100% works! Thank you so much for this, you’ve legit just opened up a whole new world of possibilities for me!!

2 Likes

Question, would it be possible to keep it the way with the task.spawn but have the alarms unsynchronized?

To where one alarm would start and then maybe half a second later another one would
or
To where one alarm would start and then a random two would start, and then another random number of alarms would light, etc. etc.

Would this be possible?

The easiest way is to change your existing code from task.spawn to task.delay and use the index as your delay time.

for i = 1, #sirens do
    task.delay(i * 0.5, function()
        -- CODE GOES HERE
    end)
end
1 Like

You guys have legit helped me out so much, to appreciate it, I’ve recorded a video of what I’ve been working on. With out the help of you this legit would have got no where.

1 Like

Glad we could help, and that delay definitely adds a lot to those sirens. Great attention to detail coming from you :slight_smile:

1 Like

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