How to make a temperature limit system

I wanted to make a temperature limit system that stops increasing or decreasing when the temperature is above 5000K or below 0K but I don’t know how.

The script:

local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp

local function temperature_rise()
	local temp = math.floor(Temperature.Value)
	Temperature.Value = temp + 50
end

while task.wait(1) do
	if Temperature.Value < 5000 then
	    temperature_rise()
	end
end

Any help is appreciated, thanks.

I think you can use the “and” keyword in your “if statement”, and as well as use the MaxTemp in it, assuming that is is an IntValue or a NumberValue.

local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp

local function temperature_rise()
	local temp = math.floor(Temperature.Value)
	Temperature.Value = temp + 50
end

while task.wait(1) do
	if Temperature.Value < MaxTemp.Value and Temperature.Value > 0 then
	    temperature_rise()
	end
end

Doesn’t work.

POOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOP

What exactly is the problem? Does the temperature go above the max value?

No, the temp doesn’t add up, it normally stops when it reached 5000.

why not use math.clamp?
unless im not getting something it should work in your favor


local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp

local function temperature_rise()
	local temp = math.floor(Temperature.Value)
	Temperature.Value = temp + 50
end

while task.wait(1) do
	if Temperature.Value <= 5000 or Temperature.Value >= 0 then
		temperature_rise()
	end
    Temperature.Value = math.clamp(Temperature.Value, Offline.Value, MaxTemp.Value)
end

unsure if offline is your minium temp but if its not just replace it with like 0 or something

Does it stop at the wrong max temperature? In your post, you mentioned you wanted it to stop at 500K.

With your code, it stops at 5K.

Consider changing the ‘5000’ to ‘500000’ on your if statement located in the while loop.

Or was the “K” short for “Kelvin”

The temperature unit i used is kelvin.

Oh, I’m sorry, I thought you meant something else. Do you want the temperature to stop on a specific number, or do you want it to continue? I’m a bit confused.

Its a reactor temperature, it stops at 5000 kelvin or 0 kelvin. then starts a meltdown or a freeze down.

The offline value is used to determine if the reactor started up or not.

I made another version of your code that has a “StartIncreasing” function and uses a for loop.

Make sure the “MaxTemp” value is set to 5000 or whatever you want it to be

When calling the function, the line after it will run when it finishes.

local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp

function StartIncreasing()
	for i = 1,MaxTemp.Value do
		Temperature.Value = Temperature.Value + 50
		task.wait(1)
	end
end

StartIncreasing()
-- When the function ends, this line of code will run, so you can replace it with anything you want.

That script still don’t work for me.

Script:

local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp

function StartIncreasing()
	for i = 1,MaxTemp.Value do
		Temperature.Value = Temperature.Value + 50
		task.wait(1)
	end
end

while task.wait() do
	StartIncreasing()
end

Replace your while loop with :

After the “StartIncreasing” function, put your meltdown code as it will run after the function finishes.

while true do
	task.wait(1)
	Temperature.Value = 0
	StartIncreasing()
	-- Meltdown script here. This line will run after the function finishes.
end

Do you mean you want to stop the while loop from running?

I mean, i want to use repeat until loops, if the temp reaches 0 or 5000, then it will stop.

The script is located in the ServerScriptService.

repeat
--temp increase code
until temperature.Value >= 5000 or temperature.Value <= 0

Like this?

Maybe try this?

local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp

while true do
	task.wait(1)
	local Result = Temperature.Value + 50
	local canbreak = false
	if Result <= 5000 then
		canbreak = false
	else
		canbreak = true
	end	
	if Result >= -1 then
		canbreak = false
	else
		canbreak = true
	end
	if canbreak == true then
		break
	else
		Temperature.Value = Result
	end
end

These scripts don’t work, It needs to run in ServerScriptService.

I edited my previous reply and made a new script.

Edited it again.