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

How to make part change BrickColor on contact?

Asked by 5 years ago
Edited 5 years ago

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.

2 answers

Log in to vote
2
Answered by
SteamG00B 1633 Moderation Voter
5 years ago
Edited 5 years ago

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.

1
I tested this too, it also works and in fact I would use this instead, thank you. Kaexotix 57 — 5y
1
I kind of wrote this a bit sloppy, so I'll see if I can also add a way to make it easier to add and remove the colors from the "colors" table. SteamG00B 1633 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
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

0
Thank you, this worked! Kaexotix 57 — 5y
0
No prob bud! SteelMettle1 394 — 5y

Answer this question