Randomize the color of a cloned brick every time it clones (New to scripting)

Hey everyone! This is my first DevFourm post so it may not be the greatest. Anyway, I have button and when it is pressed, it clones a brick and throws it into the workspace.

Script:

local clickDetector = script.Parent.ClickDetector
local objectToClone = game.Lighting.Object

function onClick()
	objectToClone:Clone().Parent = game.Workspace
end
clickDetector.MouseClick:connect(onClick)

How would I make it to where every time object gets cloned, it has a different color?

Thanks in advance!

How good for a first post?
  • Good
  • Room for improvement
  • Bad

0 voters

1 Like

pro tip: don’t put a useless poll in your post, don’t talk about it being your n-th post, etc. Just talk about the problem.

A few ways to do this. I prefer to set the color of object using the Color3.new constructor.

math.random() without any parameters produces a float between 0 and 1.
Color3.new accepts RGB values as percents of a specific primary color.

So, Color3.new(1, .5, .1) is 100% red, 50% green, 10% blue.

It also helps if we create a variable to reference the cloned object so that we can set more than one property.

local clickDetector = script.Parent.ClickDetector
local objectToClone = game.Lighting.Object

function onClick()
	local object = objectToClone:Clone()
	object.Color = Color3.new(math.random(), math.random(), math.random())
	object.Parent = game.Workspace
end
clickDetector.MouseClick:connect(onClick)
1 Like

It works! Thank you for your help and the feedback!

Also, for future posts, try searching for your problem first. This is a pretty generic problem and there are lots of posts on it.

1 Like

Before I tell you how to make each clone a different color, there are a few things you should adjust before proceeding

  1. Do not use Lighting as storage
    This is bad practice, and you should put the object into either ReplicatedStorage(for client and server access) or ServerStorage(for server only access).

  2. Do not use :connect, rather use :Connect
    I know this is kind of pedantic, but :connect is deprecated and using it is bad practice. Make sure to capitalize the ‘C’! Small things like these are noticeable by experienced coders and can say a lot!

Now, how do I change the color of a cloned brick? You need to assign the cloned object as a variable which will allow you to change its state.

local clickDetector = script.Parent.ClickDetector
local objectToClone = game.Lighting.Object

function onClick()
	local clone = objectToClone:Clone()
        clone.Parent = game.Workspace
        clone.Color = Color3.fromHSV(math.random(0,255)/255 1, 1 )
end
clickDetector.MouseClick:connect(onClick)

I recommend using Color3.fromHSV(hue, saturation, brightness) since the hue can be changed with only one number, and you can adjust the brightness/saturation with only one number as well. Hope this was helpful! If there is any confusion please let me know as this is my first answer in over a month!

2 Likes

Thank you for the help aswell! :smiley:

1 Like