Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use math.random to pick an color from an table? like:

Asked by 9 years ago

headbane = script.Parent.Parent.TMNT.Chest.Band x = game.Workspace.TMNT.Chest.Band blue = script.Parent.Parent.Colors["Bright Blue"].Value purple = script.Parent.Parent.Colors["Bright Violet"].Value orange = script.Parent.Parent.Colors["Bright Orange"].Value red = script.Parent.Parent.Colors["Bright Red"].Value quite = script.Parent.Parent.Colors:GetChildren()

colorer = {"Bright blue", "Bright red", "Bright orange", "Bright violet"} goodie = #colorer blue = 1 orange = 2 red = 3 blue = 4

script.Parent.MouseButton1Click:connect(function() x.BrickColor = headbane.BrickColor script.Parent.Color.Text = headbane.BrickColor.Name if script.Parent ~= nil then if script.Parent.MouseButton1Click then script.Disabled = true headbane.BrickColor.Value = BrickColor.new(math.random(goodie))<-----Need Help Here... end script.Disabled = false
end end)

0
If an answer is helpful you should use the green check mark to accept it! BlueTaslem 18071 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

To select a random element from a list list, we select a random index from that list:

randomIndex = math.random(1,#list)

Then ask for the element at that randomIndex:

randomElement = list[randomIndex]

In this case, randomElement is a string but we want a BrickColor from it, so we use

randomColor = BrickColor.new(randomElement)


All in one line:

randomColor = BrickColor.new(colorer[math.random(1,#colorer)])
0
Thank you for helping me love u so much :D IIxWhiteRobotxII 0 — 9y
Ad

Answer this question