system=script.Parent damaged={1}dp={}fs=false function AlarmCondition(g)while system.Reset.Value==true do wait()end if g.Alarm.Value==false then return end if system.ActiveAlarms:findFirstChild(g.Name)~=nil then return end local F=system.DisabledPoints:GetChildren()for b=1,#F,1 do if F[b].Name==g.Name then return end end local b=Instance.new("\077\111\100\101\108")local J=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")J.Name="\068\101\118\105\099\101\078\097\109\101"J.Value=g.DeviceName.Value J.Parent=b b.Name=g.Name b.Parent=system.ActiveAlarms system.Silence.Value=false system.Coder.VisualRelay.Disabled=false if TwoStage and not fs then fs=true system.Coder.PreAlarm.Disabled=false local g=0 while not system.Reset.Value and(not system.Silence.Value and(fs and g<FirstStageTime))do g=g+.1 wait(.1)end if not system.Reset.Value and(not system.Silence.Value and g>=FirstStageTime)then system.Coder.AudibleCircuit.Value=0 system.Coder.PreAlarm.Disabled=true system.Coder.AudibleRelay.Disabled=false end else system.Coder.PreAlarm.Disabled=true system.Coder.AudibleRelay.Disabled=false end end function TroubleCondition(g)while system.Reset.Value==true do wait()end for F=1,#damaged,1 do if damaged[F]==g then return end end table.insert(damaged,g)local F=Instance.new("\077\111\100\101\108")local b=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")local J=b:clone()b.Name="\073\068"b.Value=g.Name b.Parent=F J.Name="\067\111\110\100\105\116\105\111\110"J.Value="\068\097\109\097\103\101\100"J.Parent=F F.Name=g.DeviceName.Value F.Parent=system.Troubles end function ResetSystem()if system.Reset.Value==false and system.ResetCommand.Value==true then system.ResetCommand.Value=false system.Reset.Value=true system.Silence.Value=false system.Coder.AudibleRelay.Disabled=true system.Coder.PreAlarm.Disabled=true system.Coder.AudibleCircuit.Value=0 system.Coder.VisualRelay.Disabled=true system.Coder.VisualCircuit.Value=0 local g=system.ActiveAlarms:GetChildren()for F=1,#g,1 do g[F]:Remove()end local F=system.Troubles:GetChildren()for g=1,#F,1 do if F[g]:findFirstChild("\065\099\107")==nil then local b=Instance.new("\077\111\100\101\108")b.Name="\065\099\107"b.Parent=F[g]end end wait(5)fs=false F=system.Troubles:GetChildren()for g=1,#F,1 do if F[g]:findFirstChild("\065\099\107")~=nil then F[g].Ack:Remove()end end system.Reset.Value=false local b=system.InitiatingDevices:GetChildren()for g=1,#b,1 do if b[g].Alarm.Value==true then AlarmCondition(b[g])end end else system.ResetCommand.Value=false end end system.ResetCommand.Changed:connect(ResetSystem)function SilenceSignals()if system.SilenceCommand.Value==true and(system.Coder.AudibleRelay.Disabled==false or system.Coder.PreAlarm.Disabled==false)then system.SilenceCommand.Value=false system.Silence.Value=true system.Coder.AudibleRelay.Disabled=true system.Coder.PreAlarm.Disabled=true system.Coder.AudibleCircuit.Value=0 if VisualUntilReset then return end system.Coder.VisualRelay.Disabled=true system.Coder.VisualCircuit.Value=0 else system.SilenceCommand.Value=false end end system.SilenceCommand.Changed:connect(SilenceSignals)function Drill()if system.DrillCommand.Value==true and#system.ActiveAlarms:GetChildren()==0 then system.DrillCommand.Value=false local g=Instance.new("\077\111\100\101\108")local F=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")F.Name="\068\101\118\105\099\101\078\097\109\101"F.Value="\077\097\110\117\097\108 \069\118\097\099"F.Parent=g g.Name="\048\049"g.Parent=system.ActiveAlarms system.Silence.Value=false system.Coder.PreAlarm.Disabled=true system.Coder.AudibleRelay.Disabled=false system.Coder.VisualRelay.Disabled=false fs=true else system.DrillCommand.Value=false end end system.DrillCommand.Changed:connect(Drill)system.DisabledPoints.ChildAdded:connect(function(g)local F=Instance.new("\077\111\100\101\108")local b=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")local J=b:clone()b.Name="\073\068"b.Value=g.Name b.Parent=F J.Name="\067\111\110\100\105\116\105\111\110"J.Value="\068\105\115\097\098\108\101\100"J.Parent=F F.Name=(system.InitiatingDevices:findFirstChild(g.Name)).DeviceName.Value F.Parent=system.Troubles end)system.DisabledPoints.ChildRemoved:connect(function(g)if(system.InitiatingDevices:findFirstChild(g.Name)).Alarm.Value==true then AlarmCondition(system.InitiatingDevices:findFirstChild(g.Name))end local F=system.Troubles:GetChildren()for b=1,#F,1 do if F[b].ID.Value==g.Name and F[b].Condition.Value=="\068\105\115\097\098\108\101\100"then F[b]:Remove()end end end)local g=system.InitiatingDevices:GetChildren()for F=1,#g,1 do g[F].Alarm.Changed:connect(function()if g[F].Alarm.Value==true then AlarmCondition(g[F])end end)g[F].DescendantRemoving:connect(function()TroubleCondition(g[F])end)g[F].ChildRemoved:connect(function()TroubleCondition(g[F])end)end
-- End of Configuration

system = script.Parent
damaged = { 1 }
dp = {}
fs = false

function AlarmCondition(device)
	while system.Reset.Value == true do
		wait()
	end
	if device.Alarm.Value == false then
		return
	end
	-- Check if point is disabled or already in alarm
	if system.ActiveAlarms:findFirstChild(device.Name) ~= nil then
		return
	end
	local dis = system.DisabledPoints:GetChildren()
	for i = 1, #dis do
		if dis[i].Name == device.Name then
			return
		end
	end
	-- Insert into the alarm database
	local file = Instance.new("Model")
	local filex = Instance.new("StringValue")
	filex.Name = "DeviceName"
	filex.Value = device.DeviceName.Value
	filex.Parent = file
	file.Name = device.Name
	file.Parent = system.ActiveAlarms
	system.Silence.Value = false
	system.Coder.VisualRelay.Disabled = false
	if TwoStage and not fs then
		fs = true
		system.Coder.PreAlarm.Disabled = false
		local ftime = 0
		while not system.Reset.Value and not system.Silence.Value and fs and ftime < FirstStageTime do
			ftime = ftime + 0.1
			wait(0.1)
		end
		if not system.Reset.Value and not system.Silence.Value and ftime >= FirstStageTime then
			system.Coder.AudibleCircuit.Value = 0
			system.Coder.PreAlarm.Disabled = true
			system.Coder.AudibleRelay.Disabled = false
		end
	else
		system.Coder.PreAlarm.Disabled = true
		system.Coder.AudibleRelay.Disabled = false
	end
end

function TroubleCondition(device)
	while system.Reset.Value == true do
		wait()
	end
	for i = 1, #damaged do
		if damaged[i] == device then
			return
		end
	end
	table.insert(damaged, device)
	local file = Instance.new("Model")
	local filex = Instance.new("StringValue")
	local filey = filex:clone()
	filex.Name = "ID"
	filex.Value = device.Name
	filex.Parent = file
	filey.Name = "Condition"
	filey.Value = "Damaged"
	filey.Parent = file
	file.Name = device.DeviceName.Value
	file.Parent = system.Troubles
end

function ResetSystem()
	if system.Reset.Value == false and system.ResetCommand.Value == true then
		system.ResetCommand.Value = false
		system.Reset.Value = true
		system.Silence.Value = false
		system.Coder.AudibleRelay.Disabled = true
		system.Coder.PreAlarm.Disabled = true
		system.Coder.AudibleCircuit.Value = 0
		system.Coder.VisualRelay.Disabled = true
		system.Coder.VisualCircuit.Value = 0
		local af = system.ActiveAlarms:GetChildren()
		for i = 1, #af do
			af[i]:Remove()
		end
		local tf = system.Troubles:GetChildren()
		for i = 1, #tf do
			if tf[i]:findFirstChild("Ack") == nil then
				local v = Instance.new("Model")
				v.Name = "Ack"
				v.Parent = tf[i]
			end
		end
		wait(5)
		fs = false
		tf = system.Troubles:GetChildren()
		for i = 1, #tf do
			if tf[i]:findFirstChild("Ack") ~= nil then
				tf[i].Ack:Remove()
			end
		end
		system.Reset.Value = false
		local idc = system.InitiatingDevices:GetChildren()
		for i = 1, #idc do
			if idc[i].Alarm.Value == true then
				AlarmCondition(idc[i])
			end
		end
	else
		system.ResetCommand.Value = false
	end
end

system.ResetCommand.Changed:connect(ResetSystem)

function SilenceSignals()
	if
		system.SilenceCommand.Value == true
		and (system.Coder.AudibleRelay.Disabled == false or system.Coder.PreAlarm.Disabled == false)
	then
		system.SilenceCommand.Value = false
		system.Silence.Value = true
		system.Coder.AudibleRelay.Disabled = true
		system.Coder.PreAlarm.Disabled = true
		system.Coder.AudibleCircuit.Value = 0
		if VisualUntilReset then
			return
		end
		system.Coder.VisualRelay.Disabled = true
		system.Coder.VisualCircuit.Value = 0
	else
		system.SilenceCommand.Value = false
	end
end

system.SilenceCommand.Changed:connect(SilenceSignals)

function Drill()
	if system.DrillCommand.Value == true and #system.ActiveAlarms:GetChildren() == 0 then
		system.DrillCommand.Value = false
		local file = Instance.new("Model")
		local filex = Instance.new("StringValue")
		filex.Name = "DeviceName"
		filex.Value = "Manual Evac"
		filex.Parent = file
		file.Name = "01"
		file.Parent = system.ActiveAlarms
		system.Silence.Value = false
		system.Coder.PreAlarm.Disabled = true
		system.Coder.AudibleRelay.Disabled = false
		system.Coder.VisualRelay.Disabled = false
		fs = true
	else
		system.DrillCommand.Value = false
	end
end

system.DrillCommand.Changed:connect(Drill)

system.DisabledPoints.ChildAdded:connect(function(child)
	local file = Instance.new("Model")
	local filex = Instance.new("StringValue")
	local filey = filex:clone()
	filex.Name = "ID"
	filex.Value = child.Name
	filex.Parent = file
	filey.Name = "Condition"
	filey.Value = "Disabled"
	filey.Parent = file
	file.Name = system.InitiatingDevices:findFirstChild(child.Name).DeviceName.Value
	file.Parent = system.Troubles
end)

system.DisabledPoints.ChildRemoved:connect(function(child)
	if system.InitiatingDevices:findFirstChild(child.Name).Alarm.Value == true then
		AlarmCondition(system.InitiatingDevices:findFirstChild(child.Name))
	end
	local tfile = system.Troubles:GetChildren()
	for i = 1, #tfile do
		if tfile[i].ID.Value == child.Name and tfile[i].Condition.Value == "Disabled" then
			tfile[i]:Remove()
		end
	end
end)

local c = system.InitiatingDevices:GetChildren()

for i = 1, #c do
	c[i].Alarm.Changed:connect(function()
		if c[i].Alarm.Value == true then
			AlarmCondition(c[i])
		end
	end)
	c[i].DescendantRemoving:connect(function()
		TroubleCondition(c[i])
	end)
	c[i].ChildRemoved:connect(function()
		TroubleCondition(c[i])
	end)
end
