How to make a temperature limit system

use math.clamp(number x , number min , number max )

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

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

while task.wait(1) do
	temperature_rise()
    Temperature.Value = math.clamp(Temperature.Value, 0, 5000)
end

I’m pretty sure he wants the loop to end once it reaches the max value.

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

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

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

but, I want it to cool down or rise up depending on the cooling systems or the heating systems.

In my code, I added a “Rise” variable to show if it’s going to rise or not.

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

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

while task.wait(1) do
	if Rise == true then
		temperature_rise()
	else
		temperature_decrease()
	end
	Temperature.Value = math.clamp(Temperature.Value, 0, 5000)
	if Temperature.Value == 0 or Temperature.Value == 5000 then
		break
	end
end

and also depending in the “TempChanger” number value.

From what I understood, the TempChanger value shows if it’s going to rise or not.

Change

local Rise = true

to

local Rise = game.ReplicatedStorage.ReactorSystems.TempChanger

and change the if statement to

if Rise.Value == true then

FINAL RESULT

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

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

while task.wait(1) do
	if Rise.Value == true then
		temperature_rise()
	else
		temperature_decrease()
	end
	Temperature.Value = math.clamp(Temperature.Value, 0, 5000)
	if Temperature.Value == 0 or Temperature.Value == 5000 then
		break
	end
end

The TempChanger is a number value, not a bool value.

What does the “TempChanger” value show?

The TempChanger value is used to cool down or rises up by its value.

local Temperature = game.ReplicatedStorage.ReactorSystems.Temperature
local Offline = game.ReplicatedStorage.ReactorSystems.Offline
local MaxTemp = game.ReplicatedStorage.ReactorSystems.MaxTemp
local Rise = true
local TempChanger = game.ReplicatedStorage.ReactorSystems.TempChanger

local function temperature_rise()
	Temperature.Value = math.floor(Temperature.Value) + TempChanger.Value
end
local function temperature_decrease()
	Temperature.Value = math.floor(Temperature.Value) - TempChanger.Value
end

while task.wait(1) do
	if Rise == true then
		temperature_rise()
	else
		temperature_decrease()
	end
	Temperature.Value = math.clamp(Temperature.Value, 0, 5000)
	if Temperature.Value == 0 or Temperature.Value == 5000 then
		break
	end
end

I didn’t really understand what you mean, but if you want the loop to stop if the temperature is higher than 5000 or lower than 0 then i think this should help:

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 = math.clamp(temp + 50, 0, 5000)
end

while task.wait(1) do
	If Temperature.Value > 5000 or Temperature.Value < 0 then break end

	if Temperature.Value < 5000 then
	    temperature_rise()
	end
end

He wants to be able to increase or decrease the temperature by a specific value that’s contained in an instance.

Should I store the “Rise” bool in a module script so the other script can access the module.

It’s your choice. You can also place a new bool value inside of the “ReactorSystems” thing.

Did my script work?

It works, anyways thanks for solving.

Final Results:

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

local function temperature_rise()
	Temperature.Value = math.floor(Temperature.Value) + TempChanger.Value
end
local function temperature_decrease()
	Temperature.Value = math.floor(Temperature.Value) - TempChanger.Value
end

while task.wait(1) do
	if Rise.Value == true then
		temperature_rise()
	else
		temperature_decrease()
	end
	Temperature.Value = math.clamp(Temperature.Value, 0, 5000)
	if Temperature.Value == 0 or Temperature.Value == 5000 then
		break
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.