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?
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)