How do I make a part that when clicked, both makes sound and changes colour?

Hello!

I’m trying to make a part that when clicked, makes sound and changes to a specific colour (red)
I’ve tried combining two seperate scripts into one, but it hasn’t worked. The two scripts below are the ones I have tried combining into one. In regards to the part, it is inside a model along with the script, audio and a clickdetector. I have tried searching on the Developer Hub to solve this issue, but I couldn’t find a solution

  1. Script for changing colour

– script.Parent.ClickDetector.MouseClick:connect(function()
– script.Parent.BrickColor.DeepOrange() = BrickColor.Red()
– end)

  1. Script for sound

– function onclicked()
– script.Parent.ClickDetector.MaxActivationDistance = 0
– script.Parent.SFX.Playing = true
– wait(3)
– script.Parent.ClickDetector.MaxActivationDistance = 32
– end
– script.Parent.ClickDetector.MouseClick:Connect(onclicked)

You were referencing the ClickDetector and BrickColor wrong.

Here is the correct formatting for the ClickDetector:

local clickDetector = workspace.Part.ClickDetector
function onMouseClick()
-- Perform tasks here
end

clickDetector.MouseClick:connect(onMouseClick)

Correct format for BrickColor:

local Part = workspace.Part
Part.BrickColor = BrickColor.new("Bright red")

There is two way to write :Connect() function:

  • First one:
ClickDetector.MouseClick:Connect(function() -- connect the function directly
	-- script, script, script,...
end)
  • Second one:
local function someScript() -- declare the function
	-- script, script, script,...
end

ClickDetector.MouseClick:Connect(script) -- connect the function

So to combine it, you have to use one of the way above. For example:

script.Parent.ClickDetector.MouseClick:connect(function()
	script.Parent.BrickColor.DeepOrange() = BrickColor.Red()
	script.Parent.ClickDetector.MaxActivationDistance = 0
	script.Parent.SFX.Playing = true
	wait(3)
	script.Parent.ClickDetector.MaxActivationDistance = 32
end)

or:

local function onClicked()
	script.Parent.BrickColor.DeepOrange() = BrickColor.Red()
	script.Parent.ClickDetector.MaxActivationDistance = 0
	script.Parent.SFX.Playing = true
	wait(3)
	script.Parent.ClickDetector.MaxActivationDistance = 32
end

script.Parent.ClickDetector:Connect(onClicked)

Okay, here is what I have so far in the changed script

local clickDetector = workspace.Part.ClickDetector
function onMouseClick()
script.Parent.ClickDetector.MaxActivationDistance = 0
script.Parent.SFX.Playing = true
wait(3)
script.Parent.ClickDetector.MaxActivationDistance = 32
end
script.Parent.ClickDetector.MouseClick:Connect(onclicked)

There seems to be an issue with the word “onclicked” on the last line of the script, and the script analysis says that I should add an end under the first line? Also for applying the BrickColor format, where would I put this in the new script?

Please format blocks of code by wrapping it in triple backticks.

function onMouseClick()
    script.Parent.ClickDetector.MaxActivationDistance = 0
    script.Parent.SFX.Playing = true
    wait(3)
    script.Parent.ClickDetector.MaxActivationDistance = 32
end

script.Parent.ClickDetector.MouseClick:Connect(onclicked)

Your code is not working because you are trying to connect your event to a undefined function, onclicked.
Change the last line to:

script.Parent.ClickDetector.MouseClick:Connect(onMouseClick)

and it should work.

Here’s how you can change an object’s BrickColor property:

Object.BrickColor = BrickColor.new("Color Name")
Object.BrickColor = BrickColor.new("Really red")

-- Using Color3

Object.Color3 = Color3.fromRGB(RED, GREEN, BLUE)
Object.Color3 = Color3.fromRGB(153, 51, 255) --purple
script.Parent.ClickDetector.MouseClick:connect(function()
        script.Parent.Color = BrickColor.Red()
        script.Parent.SFX:Play()
end)