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:
local colorPart = script.Parent local function changeColor(part) colorPart.BrickColor = BrickColor.Random("Really blue", "Royal purple", "Bright blue") wait(0.2) end 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.
local colorPart = script.Parent local debounce = true local colors = {Color3.fromRGB(0, 97, 255), Color3.fromRGB(165, 0, 255), Color3.fromRGB(0, 225, 255)} --these functions will allow you to add/remove/edit colors easily if you plan on making this script into something bigger local function editColors(index, color)--color would be a vector3 colors[index] = Color3.fromRGB(color) end local function addColor(color)--also a vector3 colors[#colors+1] = Color3.fromRGB(color) end local function removeColor(index) table.remove(colors,index) end colorPart.Touched:Connect(function() if debounce then debounce = false local x = math.random(1,3) colorPart.Color = colors[x] end wait(1) debounce = true end)
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.
local colorPart = script.Parent local random = 0 local colorTable = {"Really blue", "Royal purple", "Bright blue"} math.randomseed(tick()) local function changeColor(part) random = math.random(1,3) print(random) colorPart.BrickColor = BrickColor.new(colorTable[random]) wait(0.2) end 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