Scripting help Color Block

So I tried to make a block that would switch to blue to red each time you clicked it but the code didn’t work. The code is very rubbish but I also don’t know what to do so please help. (also the I named the block disco if you’re confused). local Disco = game.Workspace.Disco

local Player = game.Players.LocalPlayer

Disco.Clicked:Connect(function()

end)

while Disco.Clicked == true

do Random.new(“Really red” , “Steel blue”)

end

1 Like

Clicked isn’t a Part event so it will neve fire. If you want to detect when a Part is clicked then use a ClickDetector.

local Disco = game.Workspace.Disco
local ClickDetector = Disco.ClickDetector
local Player = game.Players.LocalPlayer

ClickDetector.MouseClick:Connect(function()
	--Change color
end)

Also, use this to change the color

Disco.BrickColor = BrickColor.new("[insert colour name]")
for example: 
Disco.BrickColor = BrickColor.new("Really red")

As Rodtodon said, you need to use a ClickDetector.

The script would be something like this:

local clickDetector = script.Parent
local part = script.Parent.Parent

clickDetector.MouseClick:Connect(function()
	if part.BrickColor == BrickColor.new('Really red') then
		part.BrickColor = BrickColor.new('Steel blue')
	elseif part.BrickColor == BrickColor.new('Steel blue') then
		part.BrickColor = BrickColor.new('Really red')
	end
end)

I don’t know what the LocalPlayer is for in this script but you need a click detector inside of the part you need to click, click the part in the explorer menu
image
Click the “+” and this menu will show
image
Put “ClickDetector” in the search box at the top or click it if it already shows
Once you do that, put a script inside of the click detector which I assume you already know how to do

Then type this

local detector = script.Parent --The Click Detector
local disco = script.Parent.Parent --The Part to Color Change
local deb = false --Debounce so it doesn't switch back instantly
local debT = .5 --Debounce time, put this as whatever you want
local ran = math.random(1,2) --Chosing random color
if ran == 1 then --Checking Random
	disco.BrickColor = BrickColor.new("Steel blue") --Setting Color
else --Checking Random if not 1
	disco.BrickColor = BrickColor.new("Really red")	--Setting Color
end

detector.MouseClick:Connect(function() --Connecting function to click event
	if disco.BrickColor == BrickColor.new("Really red") then --Checking color
		if deb == false then --Checking if debounce is ready or not
			disco.BrickColor = BrickColor.new("Steel blue") --Changing color
			deb = true --Setting debounce to true
		end
	end
	if disco.BrickColor == BrickColor.new("Steel blue") then --Checking color
		if deb == false then --Checking if debounce is ready or not
			disco.BrickColor = BrickColor.new("Really red") --Changing color
			deb = true --Setting debounce to true
		end
	end
	wait(debT) --Waiting for specified time
	deb = false --Making script ready again
end)

Should work, hope this helps!

If disco is one part you will need one ClickDetector inside it.

if you want to make the disco change the color random when you click you will need to do

local on = false
disco = game.Workspace.Disco
local colors = { "Really red",
                         "Steel blue" 
                        }

disco.ClickDetector.MouseClick:Connect(function()
    if on == false then
       on = true
       while on == true do
               wait()
              local ThisColor = colors[math.random(1, #colors)]
             disco.BrickColor = colors
       end
else
        on = false
       disco.BrickColor = nil
end)

I am not on Roblox Studio so sorry if something is wrong.

I tried out all of these including yours and it still doesn’t work! It doesn’t make sense to me because this code is definetley correct but it might be a problem on my end. But thanks a lot for helping me!

Can you show output erros for we see if you get any error on output?

No problem, but can you send a screenshot or copy whatever is in your output, If we know what’s wrong we can probably help.

Yeah sure! Some of the output might be problems from other scripts in my game and plugins but here it is

ok maybe try to use Color and not BrickColor.

Also make sure this is your layout (hierarchy)
image

I just tried it and it didn’t work :frowning:

Try this script, it isn’t random but it makes the colors alternate.

local disco = script.Parent

function clicked()
	if disco.BrickColor ~= BrickColor.new('Really red') then
		disco.BrickColor = BrickColor.new('Really red')
	elseif disco.BrickColor == BrickColor.new('Really red') then
		disco.BrickColor = BrickColor.new('Steel blue')
	elseif disco.BrickColor == BrickColor.new('Steel blue') then
		disco.BrickColor = BrickColor.new('Really red')
	end
end
disco.ClickDetector.MouseClick:Connect(clicked)

Video of what’s supposed to happen: Desktop 2021.08.25 - 17.03.58.03

It FINALLY worked!! I just have one question, what’s the ~ behind the = for?

== tests for equality, while ~= tests if it’s not equal.
This is a better definition.