Facility alarm assistance

Hello there! I am trying to make my code more efficient, and I would like to make my facility alarms accommodate more than one scenario. For the facility, I have 2 scenarios: A freezedown and a meltdown. They are both triggered by a value which changes to a certain temperature. I currently have an issue where I have to make separate alarms for each scenario. I would like to make each alarm capable of turning red when the temperature value is equal to or more than 4500, and turning blue when the temperature reaches -1010. Screenshots are attached and code is also attached.

Capture

In the past, when I tried to do this, the system only checked for the 4500 to turn red and NOT the -1010 to turn blue. The code is controlled by a value at: game.Workspace.CoreScenario["Core Temperature"].

while true do wait()
  if game.Workspace.CoreScenario["Core Temperature"].Value >= 4500 then
     script.Parent.PrimaryPart = script.Parent.Main
   script.Parent:SetPrimaryPartCFrame(script.Parent.Main.CFrame * 
          CFrame.fromEulerAnglesXYZ(0.05, 0, 0))
           script.Parent.Spin.a.Brightness = 55
              script.Parent.Spin.b.Brightness = 55

else

     script.Parent.PrimaryPart = script.Parent.Main
       script.Parent:SetPrimaryPartCFrame(script.Parent.Main.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0))
          script.Parent.Spin.a.Brightness = 0
             script.Parent.Spin.b.Brightness = 0
  end
end

So first off you’ll want to utilise events properly, there’s no point running an infinite loop on a variable to detect changes. Why not just detect when a change happens, and then run the check? This will be much more efficient.

I don’t really know what all your code is doing just by looking at it, but you are never comparing the ‘Core Temperature’ value to being lower than -1010. If there are no checks for if it’s lower than -1010 then it’s never going to do anything, you need to specify that.

How about something like:

local ct = workspace.CoreScenario["CoreTemperature"]
ct.Changed:Connect(function()
	if ct.Value >= 4500 then
		--Do stuff cus its too hot
	elseif ct.Value <= -1010 then
		--Do stuff cus it's too cold
	else
		--It's somewhere in the middle, so do normal stuff.
	end
end)

Alright, I am testing your reply out right now. I didn’t know about using the elseif function to do this. Thank you for your response.

1 Like