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

Problem with tables?

Asked by 8 years ago

I have this script that I made. and it doesnt work... Sorry I'm still getting the hang of it..

local Colors = {
BrickColor.new("Lime green"),
BrickColor.new("Hot pink"),
}

script.Parent.BrickColor = [1#math.random()Colors]

2 answers

Log in to vote
1
Answered by
yelsew 205 Moderation Voter
8 years ago

What are tables?

Tables are basically dictionaries for Lua. Tables can be called either like:

local exampletable = {"I think I'm pretty", "I think I'm codey", "I think I'm helpy"}

Or like:

local exampletable = {
"I think tables are cool",
"I think tables are useful",
"I think tables are helpy"}

For your case, you will just want to put the BrickColor names as strings. Here, I'll fix your table code:

local Colors = {
"Lime green",
"Hot pink"}

What is BrickColor?

BrickColor is a constructor used in changing colors of Parts. To change it, you must do it as such:

script.Parent.BrickColor = BrickColor.new("Lime green")

But you want to implement a random color. So you would use math.random() to pick a random color out of your table. But how do we access contents of a table? Using the # operator. Yes, we are going to start a fanbase of #Colors. To implement this random color picker, just do this:

script.Parent.BrickColor = BrickColor.new(math.random(1, #Colors))

This is just a simple way of picking colors out from a table. If you want full randomness, you can use Color3.

script.Parent.BrickColor = Color3.new(math.random(1, 255) / 255, math.random(1, 255) / 255, math.random(1, 255) / 255)

This code will give you a completely random color.

Ad
Log in to vote
1
Answered by
4Bros 550 Moderation Voter
8 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
local Colors = {
BrickColor.new("Lime green"),
BrickColor.new("Hot pink"),
}


script.Parent.BrickColor  = Colors[math.random(#Colors)] 

Answer this question