Math.Random not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Getting a new number each time it repeat.

  1. What is the issue? Include screenshots / videos if possible!

The math.random only work once.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I looked at older script i used before that are still working to see if i could find what is not working. I also search topic about Math.random but still failed. And at last, i tryed to use alternative way to repeat the script like while true do.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

---///F1,F2, speed 1 Max-Min///----(Speed Stats)--Default
AMin = 25
AMax = 40
---///F1,F2, speed 2 Max-Min///----
BMin = 50
BMax = 85
---///F1,F2, speed 3 Max-Min///----
CMin = 105
CMax = 180
---///F3,    speed 1 Max-Min///----
DMin = 80
DMax = 130
---///All    speed 0 Max-Min///----
FMin = 0
FMax = 0

Stop = false

repeat
	wait(1)
	local A1Value = math.random(AMin,AMax)
	local B1Value = math.random(BMin,BMax)
	local C1Value = math.random(CMin,CMax)
	local A2Value = math.random(AMin,AMax)
	local B2Value = math.random(BMin,BMax)
	local C2Value = math.random(CMin,CMax)
	local DValue = math.random(DMin,DMax)
	local FValue = math.random(FMin,FMax)
	
	if F1.Value == 1 then
		F1.Value = A1Value
		text1.Text = F1.Value
	end
	if F2.Value == 0 then
		F2.Value = FValue
		text2.Text = 'Off'
	end
	if F2.Value == 1 then
		F2.Value = A2Value
		text2.Text = F2.Value
	end
	if F3.Value == 0 then
		F3.Value = FValue
		text3.Text = 'Off'
	end
	if F3.Value == 1 then
		F3.Value = DValue
		text3.Text = F3.Value
	end
	Total.Value = F1.Value + F2.Value + F3.Value
	Influance.Value = math.round((Total.Value - (Total.Value*2))*1.25)  
	print(Influance.Value..' Fan Influance')
until Stop == true

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

why not just use a while loop?

I already tried but it didn’t work.

Does your print statement at the end of your code run? Could you send a picture of your output

Can you send the part of the script where you define F1, text1, F2, tex2, F3, text3, Total and Influance?

Sure, here the code. Also it my first time asking for help so i forgot how to make the script in script mode.

local model = script.Parent

local F1 = model.Fan1Speed
local F2 = model.Fan2Speed
local F3 = model.Fan3Speed
local Total = model.TotalSpeed
local Influance = model.TempInfluance_Fan

—SpeedStats
local F1Speed = model.Parent.Parent.Parent.FanControl.Fan1.CurrentSpeed
local F2Speed = model.Parent.Parent.Parent.FanControl.Fan2.CurrentSpeed
local F3Speed = model.Parent.Parent.Parent.FanControl.Fan3.CurrentSpeed

—Text—
local text1 = model.Parent.Parent.Parent.ControlRoom_Down_1.Fan.F1.TempShow.SurfaceGui.TextLabel
local text2 = model.Parent.Parent.Parent.ControlRoom_Down_1.Fan.F2.TempShow.SurfaceGui.TextLabel
local text3 = model.Parent.Parent.Parent.ControlRoom_Down_1.Fan.F3.TempShow.SurfaceGui.TextLabel

—///F1,F2, speed 1 Max-Min///----(Speed Stats)–Default
AMin = 25
AMax = 40
—///F1,F2, speed 2 Max-Min///----
BMin = 50
BMax = 85
—///F1,F2, speed 3 Max-Min///----
CMin = 105
CMax = 180
—///F3, speed 1 Max-Min///----
DMin = 80
DMax = 130
—///All speed 0 Max-Min///----
FMin = 0
FMax = 0

Stop = false

repeat
wait(1)
local A1Value = math.random(AMin,AMax)
local B1Value = math.random(BMin,BMax)
local C1Value = math.random(CMin,CMax)
local A2Value = math.random(AMin,AMax)
local B2Value = math.random(BMin,BMax)
local C2Value = math.random(CMin,CMax)
local DValue = math.random(DMin,DMax)
local FValue = math.random(FMin,FMax)

if F1.Value == 0 then
	F1.Value = FValue
	text1.Text = 'Off'
end
if F1.Value == 1 then
	F1.Value = A1Value
	text1.Text = F1.Value
end
if F1.Value == 2 then
	F1.Value = B1Value
	text1.Text = F1.Value
end
if F1.Value == 3 then
	F1.Value = C1Value
	text1.Text = F1.Value
end
if F2.Value == 0 then
	F2.Value = FValue
	text2.Text = 'Off'
end
if F2.Value == 1 then
	F2.Value = A2Value
	text2.Text = F2.Value
end
if F2.Value == 2 then
	F2.Value = B2Value
	text2.Text = F2.Value
end
if F2.Value == 3 then
	F2.Value = C2Value
	text2.Text = F2.Value
end
if F3.Value == 0 then
	F3.Value = FValue
	text3.Text = 'Off'
end
if F3.Value == 1 then
	F3.Value = DValue
	text3.Text = F3.Value
end
Total.Value = F1.Value + F2.Value + F3.Value
Influance.Value = math.round((Total.Value - (Total.Value*2))*1.25)  
print(Influance.Value..' Fan Influance')
print(F1Speed.Value)
print(F2Speed.Value)
print(F3Speed.Value)

until Stop == true

Does your actual repeat command work?
Try putting a print statement right below wait(1) and see if it prints that out every time
If it only prints once, that means there is something wrong with your loop

The loop is working, i tryed to loop the print and it worked. I have no idea what is not working.

I’m a bit confused on what your issue is…
By looking at your output, it appears that a random number is being generated and printed?

Ok, i will try to re-explain it.

So the random number in script I made should by able to change value when the loop go back at the beggining.
The line bellow should would generate a different number each time (getting the same number may happend).

local A1Value = math.random(AMin,AMax)

The issue is: The number keep being the same without changing.
For exemple: First time, it get the number 76. All the following number will be 76 and it should be another number like 43,73,20…

Math.random isnt always random, sometimes it will consistently be the same number… You’ll be lucky if all of your numbers are different

Is there a way to change that so it will not alway be the same number going forever

Maybe if you used different minimum and maximums. Other than that, there’s no real way to get a different number for math.random. It’s random. Sorry

1 Like

Ok, thank you for trying to help. Maybe other developer have the solution

I just found what was going on in the script. In the small function like the one bellow.

if F3.Value == 1 then
		F3.Value = DValue
		text3.Text = F3.Value
	end

The F3.Value was the wrong value while i needed to use another value wich was F3Speed.Value

if F3Speed.Value == 1 then
		F3.Value = DValue
		text3.Text = F3.Value
	end

Sorry for the waste of time.