Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 answer

✓ Accepted answer

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:

--First, we will set the 'block' variable
local block = script.Parent

--Ok, now, we are going to tell the script to do an action if the part is touched.
script.Parent.Touched:connect(function()
	--Here inside, we would change the color.
end)

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

math.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:

--Set the 'block' variable
local block = script.Parent

--Set the activated variable, so it won't change EVERYTIME the player moves if he is touching the part
local activated = false

--Ok, now, we are going to tell the script to do an action if the part is touched.
block.Touched:connect(function()
	if activated == false then
		
		--change the activated variable to true, so the color change won't trigger again.
		activated = true
		--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)
	
		block.Color = Color3.new (math.random(0,255)/255, math.random(0,255)/255, math.random(0,255)/255)
		
	end
	
end)

--if the player stops touching the block, then activated should be false, so the player can touch again to change the color!
block.TouchEnded:connect(function()
	if activated == true then
		activated = false
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.