I'm trying to create new instances of part in a certain area. I'm using a variable to make it easy.
local position1 = math.random (-238.641, -200.141)
local position3 = math.random(-195.442, -154.443)
position 1 is on the x axis, and position 3 is on z axis. every time i create a new part and put,
NewPart1.Position = Vector3.new(position1, 30, position3)
it works! but if i do two, for example,
NewPart2.Position = Vector3.new(position1, 30, position3)
it uses the same exact random number as NewPart1
and when i run the game, they are stacked on top of each other.
Can someone solve my issue?
I've tried making other variables for NewPart2 like
local positionx = math.random (-238.641, -200.141)
local positionz = math.random(-195.442, -154.443)
and it works!, but it gets messy. and i'm trying to put 10 of those. so i'll have to do 8 more different variables. and it's really messy!
The reason it gets the same position is because math.random is global and returns the same value each time unless you randomize the seed using math.randomseed, computers use a pseudonumber generator which use a seed, and if you don't randomize the seed using math.randomseed, it will generate the same seed and hence, the same numbers. It will always generate the same number, You can either use Random.new() or use math.randomseed with math.random, You also have to use two math.random for the position because you store the number inside a variable
math.randomseed(os.time()) -- If you don't randomize this, the numbers below using math.random generate the same values, for example, if position1 is -230 in one server. The other server also has position1 -230 local position1 = math.random (-238.641, -200.141) local position3 = math.random(-195.442, -154.443) NewPart1.Position = Vector3.new(position1, 30, position3) local part2Position1 = math.random(-238.641, -200.141) local part2Position3 = math.random(-195.442, -154.443) NewPart2.Position = Vector3.new(part2Position1, 30, part2Position3)