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

How would you use a table to create a brick color change script?

Asked by 5 years ago
Edited 5 years ago

Hello, I've recently started learning how to use tables, and I am trying to practice with brick color change, but I don't know how to properly call the brick color command with a table.

This is the code, is it ok if I ask how to store the colors in a table, and call the table function with the BrickColor function?

local baseplate = game.Workspace.Baseplate while true do wait(0.5) local colors = math.random(1,5) if colors == 1 then baseplate.BrickColor = BrickColor.new("Really red") end

if colors == 2 then baseplate.BrickColor = BrickColor.new("Baby blue") end

if colors == 3 then baseplate.BrickColor = BrickColor.new("Sage green") end

if colors == 4 then baseplate.BrickColor = BrickColor.new("Bright yellow") end

if colors == 5 then baseplate.BrickColor = BrickColor.new("Neon orange") end

end

3 answers

Log in to vote
0
Answered by
Yuuwa0519 197
5 years ago
Edited 5 years ago

To get the value of the table, you put this "[]" square brackets next to the name of the table. For example, if you table is named colorTable then you will do colorTable[]. Inside of the square bracket, you will input the index of the wanted value. So, if you are trying to get the random number, put math.random() inside the square bracket.

local colorTable = {
--[[Index = 1, Value = ]]   "Really Red",
--[[Index = 2, Value = ]]   "Really Blue",
--[[Index = 3, Value = ]]   "New Yeller",
--[[Index = 4, Value = ]]   "Dark Green"
}

while wait() do
    local randomIndex = math.random(1,#colorTable)
    workspace.Baseplate.BrickColor = BrickColor.new(colorTable[randomIndex]
end

The reason inputting the index of the table returns the value is because the index is the address of the value. For example, the first value which is "Really Red" lives in address colorTable[1].

Also, in the while loop code, I includedlocal randomIndex = math.random(1,#colorTable). What the hashtag means is that when this is next to the table, it will count how many index there is inside the table. so in my example, I did #colorTable which will return 4 since there is 4 sets of data stored inside the table.

Now, that the random index is made, all it does is the script will index the value from the table. if the randomIndex returns 3, the value of colorTable[randomIndex] will be "New Yeller"

I hope my example gave you some idea of how you can use table. Good luck on your script :D

0
yo sorry for the late reply, this is amazing dude! You actually gave me the code and explained how you did it. This is one of the best advices i've seen thanks! dragonspade21 15 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Here's my solution, I typed it up so I might as well provide. I'm not good at explaining things.

baseplate = workspace:FindFirstChild("Baseplate")

colors = {
"Really red",
"Baby blue", 
"Sage green", 
"Bright yellow", 
"Neon orange"
}

while true do
    local var = math.random(1,#colors)
    if baseplate then
    baseplate.BrickColor = BrickColor.new(colors[var])
    end
    wait(0.5)
end
0
Thanks for the code, I understand it now from all the answers i've received. I appreciate taking your time to show me this. This really helped me. dragonspade21 15 — 5y
Log in to vote
0
Answered by
harstud 218 Moderation Voter
5 years ago

Probably not what you're looking for but, here's a script.

local Baseplate = game.Workspace:FindFirstChild("Baseplate")
local Colors = {

c1 = BrickColor.new("Really red"),

c2 = BrickColor.new("Baby blue"),

c3 = BrickColor.new("Sage green"),

c4 = BrickColor.new("Bright yellow"),

c5 = BrickColor.new("Neon orange")


}
while true do
   Baseplate.BrickColor = Colors.c1
   wait(0.5)
   Baseplate.BrickColor = Colors.c2
   wait(0.5)
   Baseplate.BrickColor = Colors.c3
   wait(0.5)
   Baseplate.BrickColor = Colors.c4
   wait(0.5)
   Baseplate.BrickColor = Colors.c5
   wait(0.5)
end
0
This showed me that there are many different ways to achieve the same goal, and I appreciate the code. Thanks! dragonspade21 15 — 5y
0
You're welcome. harstud 218 — 5y

Answer this question