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)
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()
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!
9 comments
before editing my answer, I recommend looking at the code context, Feahren. :)
#maps defines the amount of time the loop iterates based on the amount of children. This way you don’t have to change 3 to how many new maps you add. With additive iteration as well, only two values need to be defined, I suggest writing 1,#maps
you got discord?
I need to talk to you since it's pretty clear you don't understand what the issue is, it's easier to talk in there than here
if not, I'll just say it all here. He is selecting specifically 3 random maps, not all of them. he needs only 3 values, not the number of maps that can be randomly chosen, think of it as having many things to choose from but you only need 3 of them, a full loop of all the available things would not work.
also you had #maps, 1 which would error even if the context was correct because you did not add -1 as the third parameter of the loop so it would not start at all, consider removing the downvote cos what you did was completely wrong.
@Feahren It is the NUMBER OF TIMES the loop will iterate, not the AMOUNT OF TIME it will take.
no one care about my more indepth answer? :(
Right, I understand, my bad. I am aware of the semantic error I would’ve caused as well. I clearly resolved that in my previous comment