Is there a more efficient way to handle this?

Hi there!

Here’s the code:

local rs = game:GetService("ReplicatedStorage")
local temp = rs:FindFirstChild("Temperature")
local duration = rs:FindFirstChild("Duration")

temp.Changed:Connect(function()
	if temp.Value <= 87 or temp.Value == 80 then
		duration.Value = 0.7500
	elseif temp.Value == 93 or temp.Value > 87 then
		duration.Value = 0.6000
	elseif temp.Value == 97 or temp.Value > 93 then
		duration.Value = 0.4500
	elseif temp.Value == 103 or temp.Value > 97 then
		duration.Value = 0.25000
	elseif temp.Value == 124 or temp.Value > 110 then
		duration.Value = 0.05
	end
end)

I’m working on a script that works with two number values: Temperature and Duration, both of which are located in the ReplicatedStorage while this script is located in the ServerScriptService.

There is a separate script that is randomizing the temperature value every now and then. I’m trying to have this script determine the duration value based on the temperature.

Examples:
If the temperature is 113, I want the duration to be 0.05.
If the temperature is 89, I want the duration to be 0.6.

I think you get the idea.

The only problem I’m encountering is that these numbers for the temperature are randomized, and while in this script I’m trying to predict these values, it comes at a negative expense of negating other intervals of conditional statements I’ve set up.

With the current script posted on here, the duration value currently says for a 116 temperature:

0.5999999999999999778

My question: what is a more efficient way to deal with this sort of interval problem when I’m creating continuous conditional statements that can possibly negate the other?

It’s getting the number from here. It’s because the OR will be triggering. I recommend that you use math to solve this issue though instead of making a bunch of ifs and elseifs as it will become hard to maintain later on.

You should be using the and operator instead of or

temp.Changed:Connect(function()
	if temp.Value >= 80 and temp.Value <= 87 then
		duration.Value = 0.7500
    elseif...
end)
3 Likes

Thanks so much! Appreciate it!

1 Like