Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need help with a math.random function(?)

Asked by 5 years ago

So here's my script

01Var1 = math.random(1,3)
02 
03function clicked()
04 
05    if Var1 == 1 then
06        Instance.new("Part",game.Workspace)
07    elseif Var1 == 2 then
08        Instance.new("TrussPart",game.Workspace)
09    elseif Var1 == 3 then
10        Instance.new("ParticleEmitter",game.Workspace)
11    else
12        print("Nothing has happen")
13    end
14end
15 
16script.Parent.ClickDetector.MouseClick:Connect(clicked)

Whenever I start the "Play" I click the clickdetector and only spawns only one of the random 3 parts.. like if I started the game and then spammed clickdetector it would only do number 2 which would be TrussPart and another time when I press "Play" it would spawn number 1 which is Part.

How can I do it so whenever I press the Clickdetector it makes it so that it chooses 1,2 or 3 randomly instead of being stuck on one of the numbers.

0
line 2 put math.randomseed() greatneil80 2647 — 5y
0
It still doesn't work, and I've searched up how to use it but theres still no solutions e.e menofmen1234 20 — 5y
0
Oh yeah the guy below in the answers is right, your random number is outside the event so it never changes, to fix it, put it in the function greatneil80 2647 — 5y

2 answers

Log in to vote
1
Answered by
PastDays 108
5 years ago
Edited 5 years ago

Because the math.random is outside the function it only fires once, you need it to fire everytime to give a new number so make it fire when the function is fired.

01function clicked()
02    Var1 = math.random(1,3)
03 
04    if Var1 == 1 then
05        Instance.new("Part",game.Workspace)
06    elseif Var1 == 2 then
07        Instance.new("TrussPart",game.Workspace)
08    elseif Var1 == 3 then
09        Instance.new("ParticleEmitter",game.Workspace)
10    else
11        print("Nothing has happen")
12    end
13end
14 
15script.Parent.ClickDetector.MouseClick:Connect(clicked)
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

math.random() usage is now discouraged, because even since it was re-implemented with to use the new PRNG, it's using a shared instance, so even when seeded you can't make it deterministic for testing and debugging purposes. It also redirects to the new Random class through a bunch of conditional logic in order to support all the variants of arguments math.random() could take and map them to the new calls. So better off to just use the new system yourself.

It's not more any more lines of code because the constructor is the randomseed function, and you use it basically the same way once you have an instance of the generator.

Example:

1local TYPES = { "Part", "TrussPart", "ParticleEmitter" }
2local RNG = Random.new(tick()) -- Constructs and seeds the PRNG
3local function Clicked()
4    Instance.new(TYPES[RNG:NextInteger(1,3)], game.Workspace)
5end
6script.Parent:FindFirstChildOfClass("ClickDetector").MouseClick:Connect(Clicked)

I've also removed the unnecessary use of conditional logic and comparisons to index into a finite set of options.

Answer this question