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

How do I make a script changes the colour of a brick to whatever it touches? [SOLVED]

Asked by 10 years ago

I want a script so say BlockA is Green and BlockB is Blue, BlockB then touches BlockA, BlockA turns blue. But if BlockA was then to change colour, BlockB would be the same?

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

The color of a part is specified by its BrickColor property.

To get or set the color of a part, we get / set its BrickColor property.


The brick you want to change colors I call colorblock here; it could be script.Parent or workspace.Flashing or anything else you want.

When something touches colorblock, it fires its Touched event.

When the Touched event fires, it passes a parameter of the part touching colorblock. We get that touching part's BrickColor, and set colorblock's BrickColor to that.

It could look like this:


function BlockTouched(partTouching)
    local newColor = partTouching.BrickColor;
    colorblock.BrickColor = newColor;
    -- `partTouching` is a parameter
    -- passed that is the part that touched
    -- `someblock`.
    -- We just copy its `BrickColor` to `someblock`'s `BrickColor`.
end

colorblock.Touched:connect(BlockTouched);

Where someblock is a reference to the brick which will change color.




If we want to ignore certain touches, like, for instance, the base plate, we would have to check whether or not the object is one we want (or alternatively anchor it, so that it doesn't touch other anchored objects):

function BlockTouched(partTouching)
    if partTouching.Name ~= "BasePlate" then
        local newColor = partTouching.BrickColor;
        colorblock.BrickColor = newColor;
        -- Ignore parts named BasePlate
    end
end

colorblock.Touched:connect(BlockTouched);

Or we could also check that partTouching isn't anchored:

if not partTouching.Anchored then

(although this may not be what you want)

0
I still don't get it :P Michael007800 144 — 10y
0
I edited it to my needs yet it still doesn't work... Michael007800 144 — 10y
0
I tested just to make sure. It works completely. BlueTaslem 18071 — 10y
0
Made a quick change. It works perfect now! (Needed to add a local colorblock) Michael007800 144 — 10y
View all comments (2 more)
0
But now the baseplate affects it... Michael007800 144 — 10y
0
Yes, because this doesn't care *what* touches it. I will amend this answer showing a way to exclude certain parts. BlueTaslem 18071 — 10y
Ad

Answer this question