system=script.Parent damaged={1}dp={}fs=false function AlarmCondition(a)while system.Reset.Value==true do wait()end if a.Alarm.Value==false then return end if system.ActiveAlarms:findFirstChild(a.Name)~=nil then return end local s=system.DisabledPoints:GetChildren()for z=1,#s,1 do if s[z].Name==a.Name then return end end local z=Instance.new("\077\111\100\101\108")local i=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")i.Name="\068\101\118\105\099\101\078\097\109\101"i.Value=a.DeviceName.Value i.Parent=z z.Name=a.Name z.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 a=0 while not system.Reset.Value and(not system.Silence.Value and(fs and a<FirstStageTime))do a=a+.1 wait(.1)end if not system.Reset.Value and(not system.Silence.Value and a>=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(a)while system.Reset.Value==true do wait()end for s=1,#damaged,1 do if damaged[s]==a then return end end table.insert(damaged,a)local s=Instance.new("\077\111\100\101\108")local z=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")local i=z:clone()z.Name="\073\068"z.Value=a.Name z.Parent=s i.Name="\067\111\110\100\105\116\105\111\110"i.Value="\068\097\109\097\103\101\100"i.Parent=s s.Name=a.DeviceName.Value s.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 a=system.ActiveAlarms:GetChildren()for s=1,#a,1 do a[s]:Remove()end local s=system.Troubles:GetChildren()for a=1,#s,1 do if s[a]:findFirstChild("\065\099\107")==nil then local z=Instance.new("\077\111\100\101\108")z.Name="\065\099\107"z.Parent=s[a]end end wait(5)fs=false s=system.Troubles:GetChildren()for a=1,#s,1 do if s[a]:findFirstChild("\065\099\107")~=nil then s[a].Ack:Remove()end end system.Reset.Value=false local z=system.InitiatingDevices:GetChildren()for a=1,#z,1 do if z[a].Alarm.Value==true then AlarmCondition(z[a])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 a=Instance.new("\077\111\100\101\108")local s=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")s.Name="\068\101\118\105\099\101\078\097\109\101"s.Value="\077\097\110\117\097\108 \069\118\097\099"s.Parent=a a.Name="\048\049"a.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(a)local s=Instance.new("\077\111\100\101\108")local z=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")local i=z:clone()z.Name="\073\068"z.Value=a.Name z.Parent=s i.Name="\067\111\110\100\105\116\105\111\110"i.Value="\068\105\115\097\098\108\101\100"i.Parent=s s.Name=(system.InitiatingDevices:findFirstChild(a.Name)).DeviceName.Value s.Parent=system.Troubles end)system.DisabledPoints.ChildRemoved:connect(function(a)if(system.InitiatingDevices:findFirstChild(a.Name)).Alarm.Value==true then AlarmCondition(system.InitiatingDevices:findFirstChild(a.Name))end local s=system.Troubles:GetChildren()for z=1,#s,1 do if s[z].ID.Value==a.Name and s[z].Condition.Value=="\068\105\115\097\098\108\101\100"then s[z]:Remove()end end end)local a=system.InitiatingDevices:GetChildren()for s=1,#a,1 do a[s].Alarm.Changed:connect(function()if a[s].Alarm.Value==true then AlarmCondition(a[s])end end)a[s].DescendantRemoving:connect(function()TroubleCondition(a[s])end)a[s].ChildRemoved:connect(function()TroubleCondition(a[s])end)end


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
