Math isn't adding up

I am trying to make a value named “Temp” go up when the coolant value is 0-2, but down if the coolant value is 3 or 4. For some reason, however, it either outputs 2 or -2 at a coolant value of 4. This should not be happening. When I use a calculator, it shows -2 to -1.6 depending on what you put substituting math.random()

Here is the code:

local temp = workspace:WaitForChild("CoreData"):WaitForChild("Temp")
local coolant = workspace:WaitForChild("CoreData"):WaitForChild("Coolant")
local tempaccel = workspace:WaitForChild("CoreData"):WaitForChild("TempAccel")
local running = workspace:WaitForChild("CoreData"):WaitForChild("Running")
local meltdown = workspace:WaitForChild("CoreData"):WaitForChild("Meltdown")

while wait(0.5) do
	math.randomseed(os.clock())
	if running.Value == true then
		if meltdown.Value == false then
		print((coolant.Value * math.random(-1.00,-0.90)) + 2)
		end
		temp.Value += tempaccel.Value
	end
end

Is there a problem in my code that I’m not seeing or is this a bug of some sort?

1 Like

This might be your issue, math.random can only return whole numbers, so this will always return 0 no matter what. Try typing this into your command bar and see that it always prints 0:

print(math.random(-1.00,-0.90))

To fix this, try multiplying your minimum and maximum values by some crazy big number like 100000, then divide it by that same number outside math.random, like this:

print(math.random(-1.00 * 100000, -0.90 * 100000)/100000)
1 Like

This worked very well, I didn’t know that math.random() could only output whole numbers! In addition, I found that the “TempAccel” value was an IntValue. Thank you!

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