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)
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.
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)
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)
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?
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.