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
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)
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
Click the “+” and this menu will show
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)
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!
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)