Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a part that makes a sound when you click it?

Asked by 5 years ago

I am just starting on scripting and I was wondering how to make this. So what I do is just walk up to the part, click it and it makes a noise. How do I do this?

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

What you'll need for this is a ClickDetector, which will fire an event when the player clicks on it. Simply connect a function to the event, and the function will be ran whenever the event is fired. Also take note of the method named FindFirstChildOfClass, which finds the first child of the given type, in this case Sound. After we've gotten the sound, we can simply call its Play method whenever we want to play it.

--This code is in a Script, which is in the desired part.
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent
local sound = script.Parent:FindFirstChildOfClass("Sound")
clickDetector.MouseClick:Connect(function()
    sound:Play()
end)
0
How do i make it play the sound i want it to play? CreatorOfStuff2001 25 — 5y
0
If you have the sound in the explorer, simply child it to the part (drop the sound on top of it), and the script will find it. davidgingerich 603 — 5y
0
It's not working... am I just not seeing something here? CreatorOfStuff2001 25 — 5y
0
Have you made sure the sound is a child of the part? Can you send a pic? davidgingerich 603 — 5y
Ad
Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

put a click detector object inside of the part and use the click detector clicked event

--function that gets called whenever the clickdetector gets clicked:
game.Workspace.PartNameHere.ClickDetector.MouseClick:Connect(function()
    game.Workspace.Sound:Play() --play sound
end)

Here's a tutorial about events: https://www.youtube.com/watch?v=oAnWzxol4e8 I'd recommend watching the whole series since you're knew. It's a good resource.

Answer this question