Need help with preventing an intvalue going into negatives (below 0)

What I am trying to achieve is a temperature screen displaying the current temperature of an object, the temperature is displayed in kelvin and since kelvin does not go below 0 I have to prevent the intvalue from going under 0, so I added a server script as a child to the intvalue. (These are stored in the workspace, grouped in a folder)


The main issue with this may be the code, so that’s why I’m posting this here to maybe get a more efficient way or solve an error with the current code.


I have tried looking for a different method, or looking on other dev forums, anything I found didn’t work as intended.

In case this is helpful at all I will include the code that was used for displaying the temperature on the screen.


Also mentioning that the temperature goes up in an i loop with a random increment.


Any helpful responses are much obliged.

When you set the amount, try using math.max instead of checking after its set. For example:

temp.Value = math.max(0, temp.Value - math.random(3,5))

You could use math.clamp as well if you want an upper limit to the temperature as well. Example:

temp.Value = math.clamp(temp.Value - math.random(3,5),0,10000) 
-- it will always be between 0 and 10000

use the math.clamp function, math.clamp(value, min, max)


I don’t think you need a script to make sure an IntValue stays within limits; There’s a new instance type called an IntConstrainedValue, which lets you set the upper and lower bounds, and it will automatically keep it between them.
Screenshot 2022-11-12 113226


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