In my game, when the player steps onto a brick, the brick color changes randomly through selected colors, and stops at the color it was last on when the player got off. I tried to use this script:
1 | local colorPart = script.Parent |
2 | local function changeColor(part) |
3 | colorPart.BrickColor = BrickColor.Random( "Really blue" , "Royal purple" , "Bright blue" ) |
4 | wait( 0.2 ) |
5 | end |
6 | colorPart.Touched:Connect(changeColor) |
But instead, the brick changed to other colors besides the selected. Please help.
So the problem with using BrickColors is that the random function only chooses a random color from all the colors. So what I put those colors into a table and had a random function choose the index instead, then it would assign the color chosen to the part. I also added a debounce for your touched event for you.
01 | local colorPart = script.Parent |
02 | local debounce = true |
03 | local colors = { Color 3. fromRGB( 0 , 97 , 255 ), Color 3. fromRGB( 165 , 0 , 255 ), Color 3. fromRGB( 0 , 225 , 255 ) } |
04 |
05 | --these functions will allow you to add/remove/edit colors easily if you plan on making this script into something bigger |
06 |
07 | local function editColors(index, color) --color would be a vector3 |
08 | colors [ index ] = Color 3. fromRGB(color) |
09 | end |
10 |
11 | local function addColor(color) --also a vector3 |
12 | colors [ #colors+ 1 ] = Color 3. fromRGB(color) |
13 | end |
14 |
15 | local function removeColor(index) |
I have tested this, it does work, and if it solves your problem, then please mark it as the accepted answer.
Also, I just picked random rgb values that came close to the colors you specified, if they aren't exactly what you want you can tweak them, and I recommend using this tool to help you pick the colors.
01 | local colorPart = script.Parent |
02 | local random = 0 |
03 | local colorTable = { "Really blue" , "Royal purple" , "Bright blue" } |
04 | math.randomseed(tick()) |
05 |
06 | local function changeColor(part) |
07 | random = math.random( 1 , 3 ) |
08 | print (random) |
09 | colorPart.BrickColor = BrickColor.new(colorTable [ random ] ) |
10 | wait( 0.2 ) |
11 | end |
12 |
13 | colorPart.Touched:Connect(changeColor) |
this should do exactly what you want and will randomly give you one of the three colors.
I do suggest adding in a debounce though so that it doesn't flash a bunch of colors