DrRbx
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 12 |
|
|
| 19 Feb 2016 07:46 AM |
I have my maps in a model in ServerStorage, and I want my game to pick a different random map every time. This is the code that is inside the main game loop: local allmaps = maps:GetChildren() math.randomseed(tick()) local newmap = allmaps[math.random(#allmaps)]:clone() newmap.Parent = mapholder Every round it picks the same map and I cant seem to figure out why. Help?
|
|
|
| Report Abuse |
|
|
Craftero
|
  |
| Joined: 24 Jun 2011 |
| Total Posts: 1451 |
|
|
| 19 Feb 2016 07:52 AM |
I'm pretty sure this is the solution:
You have to have a range of numbers for math.random to work. So in this case, you want it to pick map 1, map 2 or map 3, etc. This means you have to specify what number you want to start counting from, which is 1 in this case.
To do this, we do as follows:
local newmap = allmaps[math.random(1, #allmaps)]:clone()
Hope this helps. Let us know if it didn't work for you. |
|
|
| Report Abuse |
|
|
DrRbx
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 12 |
|
|
| 19 Feb 2016 07:56 AM |
No that didn't work, it seems to pick something different every time the script runs, so when I test it, but from then it carries on picking the same.
|
|
|
| Report Abuse |
|
|
parkiet3
|
  |
| Joined: 16 Jul 2011 |
| Total Posts: 832 |
|
|
| 19 Feb 2016 10:29 AM |
Add this to the top
math.randomseed(tick()) |
|
|
| Report Abuse |
|
|
parkiet3
|
  |
| Joined: 16 Jul 2011 |
| Total Posts: 832 |
|
| |
|
71428
|
  |
| Joined: 06 Aug 2015 |
| Total Posts: 339 |
|
|
| 19 Feb 2016 10:34 AM |
Unfortunately ROBLOX is not as random as people would like it to be.
You could try something like this after randomseed
for i = 1, math.random(1, 10) wait(math.random(1, 10) * 0.01) end
Just to help it get started off. Not sure if that would fix your issue but you can try. |
|
|
| Report Abuse |
|
|
|
| 19 Feb 2016 10:39 AM |
| ... Is there only one map in the folder? |
|
|
| Report Abuse |
|
|
DrRbx
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 12 |
|
| |
|
Warlyx
|
  |
| Joined: 08 Feb 2016 |
| Total Posts: 120 |
|
|
| 19 Feb 2016 12:01 PM |
This may be your problem
local random = randoms[math.random(1,#randoms)]
while wait() do random:clone().Parent = workspace end
See it will always be the random it chooses, unless you put it inside of the while wait() do, which is an ongoing loop starting over selecting it at random. What I'm saying is, if you want it to select at random, you will need to put it inside of a function as the variable is just stating it needs to have a spot so it can select randomly more than once.
________________________________________________________________________ |
|
|
| Report Abuse |
|
|
Warlyx
|
  |
| Joined: 08 Feb 2016 |
| Total Posts: 120 |
|
|
| 19 Feb 2016 12:02 PM |
therefore it should be;
local randoms = {workspace.Part1,workspace.Part2}
while wait() do local random = randoms[math.random(1,#randoms)]
random:clone().Parent = workspace end
________________________________________________________________________ |
|
|
| Report Abuse |
|
|
|
| 19 Feb 2016 12:09 PM |
Everything OP wrote is fine. I'm not sure what is wrong with randomseed(). I was getting different results in initial start before, but recently it's been outputting the same value.
I think the only workaround for now is to separate your newmap variable (default to nil) and assign it after running your map selection through 2 iterations.
|
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 19 Feb 2016 12:22 PM |
math.randomseed(tick()%1*1e6) local allmaps = maps:GetChildren() local newmap = allmaps[math.random(#allmaps)]:clone() newmap.Parent = mapholder
Issues boil down to MSBs biasing the LCG, tick() is a terrible source for a seed, setting the seed over and over doesn't make things more random, yada yada yada |
|
|
| Report Abuse |
|
|
|
| 19 Feb 2016 12:27 PM |
Works perfectly. Thanks for sharing, Casualist.
|
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 19 Feb 2016 12:36 PM |
| Anytime, sorry if I came off a little blunt. Kinda tired and I usually answer this question every other day. If anyone would like an in depth explanation I'll be happy to write one up proper later in the day |
|
|
| Report Abuse |
|
|