So, im making this minigame type of game, and i need all the maps to be different. I tried using a loop to find a new random value, but it makes the game laggy and does not seem very efficient. I found something called Random.new() on the devforum, but have no idea how to use it. here's what i tried so far.
local maps = game.ReplicatedStorage.Maps:GetChildren() local map1 = Random.new(1,#maps) local map2 = Random.new(1,#maps) local map3 = Random.new(1,#maps) script.Map1.Value = map1.Name script.Map2.Value = map2.Name script.Map3.Value = map3.Name
Hey, try this and see if it works fine :)
local maps = game.ReplicatedStorage.Maps:GetChildren() for i = 1, 3, 1 do script["Map"..i].Value = math.random(1, #maps) end
Additionally, if you want the maps that have been chosen already to not appear again, you will need to use tables.
here's how I'd do it.
local maps = game.ReplicatedStorage.Maps:GetChildren() for i = 1, 3, 1 do map = math.random(1,#maps) script["Map"..i].Value = map table.remove(maps, map) end
This will remove the map that has already been chosen so the next one that gets selected will not be the same.
That my friend is not how the Random
class works. Let me explain.
The Random
class allows you to create a new random object and you can also specify an optional seed. Such that: rand Random.new([string | float Seed])
using this newly created random object you have the choice of 2 functions one is: int rand:NextInteger(int Min, int Max)
which returns a random integer between min and max. The other function you have is: float rand:NextNumber([float Min = 0, float Max = 1])
this returns a random float between min and max which will be 0 to 1 if not specified. The random object also has another function rand rand:Clone()
which returns a new random object with the same seed and internal iteration.
So here is how you would use it:
local rand = Random.new() local randomInteger = rand:NextInteger(0, 10) print(randomInteger) -- Output: 1, 2, ... 10
And I know this isn't a request site, but for your case you would do this:
local maps = game.ReplicatedStorage.Maps:GetChildren() local rand = Random.new() -- You also need to index the "maps" table in order to get the objects, otherwise you would just get a number between 1 and the number of maps local map1 = maps[rand:NextInteger(1, #maps)] local map2 = maps[rand:NextInteger(1, #maps)] local map3 = maps[rand:NextInteger(1, #maps)] script.Map1.Value = map1.Name script.Map2.Value = map2.Name script.Map3.Value = map3.Name
The above code can be simplified to:
local maps = game.ReplicatedStorage.Maps:GetChildren() local rand = Random.new() for i = 1, 3 do local map = maps[rand:NextInteger(1, #maps)] local value = script:FindFirstChild("Map"..i) if value then value.Value = map.Name end end
I hope this helps!
Can't you set a variable to a random integer.
local map = math.random(1,10) If map == 1 then Elseif map == 2
ECT.