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?
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)
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.