How to change different color every time a Player clicks my Model?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

How to change different color every time a Player clicks my Model? 1st click = Red, 2nd click = Orange, 3rd click = Yellow, 4th click = Green, 5th click = Blue, 6th click = Indigo, 7th click = Violet, Repeat.

  1. What is the issue? Include screenshots / videos if possible!

How to change different color every time a Player clicks my Model?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I haven’t tried any solutions so far. I did look for solutions on the Developer Hub.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block
function scriptParentMouseClickConnect()
	script.Parent.Parent.BrickColor = BrickColor.Blue()
end
script.Parent.MouseClick:Connect(scriptParentMouseClickConnect)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

4 Likes

I assume the script is in the click detecter and the parent of the parent is the model

local click = script.Parent
local color = 1
click.MouseButton1Click:Connect(function()
if color = 1 then
click.Parent.PartOfModel1.BrickColor = color3.new()
color = color + 1
end
--and do the rest of the if statements
end)
2 Likes

oh and one bad thing about changing a color of models is that you gotta change them one by one

2 Likes

any colors

script.Parent.MouseClick:Connect(function()
	script.Parent.Parent.Color = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))
end)
1 Like

You can approach this in 2 ways. One thing that should remain consistent, though, is if it’s 7 then revert it back to 1:

1- use Color3.fromHSV and make it increment by 1 each time.

local value = 0

script.Parent.ClickDetector.MouseClick:Connect(function()
	value += 1 -- increment by 1
	script.Parent.Color = Color3.fromHSV(value / 7, 1, 1)
	if value == 7 then
		value = 0
	end
end)

2- use tables

local myColours = {
    Color3.new(1,0,0);
    Color3.new(1,0.5,0);
    Color3.new(1,1,0);
    Color3.new(0,1,0);
    Color3.new(0.25,0,1);
    Color3.new(0.5,0,1)
}
local amount = 0
script.Parent.ClickDetector.MouseClick:Connect(function()
    amount += 1 -- add 1 to the amount
    script.Parent.Color = myColours[amount] -- use the index of the table to find the amount
    if amount == #myColours then
        amount = 0
    end
end)
1 Like

It only does the last color.

local value = 0

script.Parent.MouseClick:Connect(function()
	value += 1 -- increment by 1
	script.Parent.Parent.Color = Color3.fromHSV(1, 0, 0)
	value += 1
	script.Parent.Parent.Color = Color3.fromHSV(1, 0.647059, 0)
	value += 1
	script.Parent.Parent.Color = Color3.fromHSV(1, 1, 0)
	value += 1
	script.Parent.Parent.Color = Color3.fromHSV(0, 1, 0)
	value += 1
	script.Parent.Parent.Color = Color3.fromHSV(0, 0, 1)
	value += 1
	script.Parent.Parent.Color = Color3.fromHSV(0.294118, 0, 0.509804)
	value += 1
	script.Parent.Parent.Color = Color3.fromHSV(0.560784, 0, 1)
	if value == 7 then
		value = 0
	end
end)

You should only do it once.

On a side note, I would highly recommend not doing long strands of elseifs, I think I read somewhere that indexing a table is much faster than constantly checking conditions. This difference is usually negligible though, but it would be a good idea to get into the routine of doing things with tables and whatnot.

If you’re new to programming though, and you want to just get the basics down and not relying on sort of “creative” (for lack of a better word) ways, using if and elseif statements does work, it just isn’t seen as a good practice.

1 Like
local Colors = {
	Color3.new(0.666667, 0, 0), -- Red
	Color3.new(1, 0.333333, 0), -- Orange
	Color3.new(1, 1, 0), -- Yellow
	Color3.new(0, 0.333333, 0), -- Green
	Color3.new(0, 0, 1), -- Blue
	Color3.new(0, 0, 0.498039), -- Indigo
	Color3.new(0.666667, 0, 1) -- Violet
}

local CurrentNumber = 1

while true do
	script.Parent.MouseClick:Wait()
	script.Parent.Parent.Color = Colors[CurrentNumber]
	CurrentNumber += 1
	if CurrentNumber >= #Colors +1 then
		CurrentNumber -= #Colors 
	end
end
3 Likes