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

How Do I can make a block change his color when it is touched?

Asked by 7 years ago

I ask this because I want to make a championship of more painted blocks.

1 answer

Log in to vote
0
Answered by
OfcPedroo 396 Moderation Voter
7 years ago

StevenUniverse, I will help you!!!

Ok, say you have a part (the block) in the workspace, ok?

You wanna insert a script in the part, and write this in:

1--First, we will set the 'block' variable
2local block = script.Parent
3 
4--Ok, now, we are going to tell the script to do an action if the part is touched.
5script.Parent.Touched:connect(function()
6    --Here inside, we would change the color.
7end)

Now here's the problem. I don't know which color you want to put in, so I'm just going to put it color change randomly.

To make a random number, you have to use

1math.random(0, 255) --Inside you can put extreme values, for example math.random(1, 2), there, the value would always be between 1 and 2. I wrote 0, 255, because a color can go from 0 to 255 in each rgb value.

So, if you want the part's color to change to a random value, the final script would be this one:

01--Set the 'block' variable
02local block = script.Parent
03 
04--Set the activated variable, so it won't change EVERYTIME the player moves if he is touching the part
05local activated = false
06 
07--Ok, now, we are going to tell the script to do an action if the part is touched.
08block.Touched:connect(function()
09    if activated == false then
10 
11        --change the activated variable to true, so the color change won't trigger again.
12        activated = true
13        --Here inside, we would change the color to a random one: We will change the "r" to a random number, just like the "g" and "b" (rgb = red, green, blue and it's a color pattern)
14 
15        block.Color = Color3.new (math.random(0,255)/255, math.random(0,255)/255, math.random(0,255)/255)
View all 26 lines...
Ad

Answer this question