The math.random function returns random numbers based on the given arguments. It can work in 3 ways:
No arguments:
print(math.random())
One argument:
print(math.random(5))
Two arguments
print(math.random(5,10))
This algorithm can be used in a lot of useful ways, especially when creating a game based off random events. But, this function only returns a numerical value. Which means you have to base your random events off the numerical value it returns.
I'll give an example in the form of your scenario:
local Maps = game:GetService("ServerStorage").Maps:GetChildren()
local RandomMap = Maps[math.random(#Maps)]
GetChildren() returns a table of children from the parent it's called on. You can index this table with a number. In this case, i indexed it with a random number. Giving me a random key (map).
Edit:
And yes, you can use this with various types of Colors, such as creating a random BrickColor, or creating a random Color3 color (for Guis, Effects, and anything that has a Color or Color3 property). Though these methods don't involve using the "random" function from the "math" table.
Here are some examples:
Using BrickColor.Random():
local p = Instance.new("Part",workspace)
local RandomBrickColor = BrickColor.Random()
p.BrickColor = RandomBrickColor
You can also convert this random brick color to a Color3 value, so it can apply to any object that has a Color3 property:
local RandomColor3 = BrickColor.Random().Color
script.Parent.BackgroundColor3 = RandomColor3
Though, most people tend to stick to the method of calling math.random 3 times in place of the RGB values of Color3 like this:
local RandomColor3 = Color3.new(math.random(), math.random(), math.random())
script.Parent.BackgroundColor3 = RandomColor3
But either way works, so it all depends on what you like best. Hope this helped.
4 comments
Thank you! That Helps Alot! But Is It Possible To Make Point Lights And Such do a random Color?
Yup. There's a function already defined for returning a random BrickColor, so you can use that and convert it to a Color3 by using ".Color". I'll update the answer and give an example
Alright Thank You So Much!
No problem, glad it helped.