so i have this script here in a clickdetector, you click a button and a door opens! how can i make a sound play when the button is clicked?
open=true script.Parent.MouseClick:connect(function() if open==true then open=false script.Parent.Parent.Parent.Door.Transparency=1 script.Parent.Parent.Parent.Door.CanCollide=false script.Parent.Parent.BrickColor=script.ButtonC.Value else open=true script.Parent.Parent.Parent.Door.Transparency=0 script.Parent.Parent.Parent.Door.CanCollide=true script.Parent.Parent.BrickColor=script.ButtonO.Value end end)
Insert a 'sound' object inside of the part with the clickdetector and do
open=true sound1 = script.Parent.Parent.Sound1 -- Reference the sound object sound2 = script.Parent.Parent.Sound2 -- Reference the sound object script.Parent.MouseClick:connect(function() if open==true then open=false script.Parent.Parent.Parent.Door.Transparency=1 script.Parent.Parent.Parent.Door.CanCollide=false script.Parent.Parent.BrickColor=script.ButtonC.Value sound1:Play() -- The method :Play will play the sound else open=true script.Parent.Parent.Parent.Door.Transparency=0 script.Parent.Parent.Parent.Door.CanCollide=true script.Parent.Parent.BrickColor=script.ButtonO.Value sound2:Play() end end)
Make sure you put the soundid in the soundid property inside the sound object.
https://developer.roblox.com/en-us/api-reference/function/Sound/Play
For these scripts, simply indicate the parent of the child and how you want it to function (except for the new instance part.) For example, game is always the first parent. What is inside of the game is Workspace, etc. What is inside of Workspace is... and so on. The child is what is inside of the parent (Workspace is the child of the game...)
You could create a new instance (Thing) like so:
Instance.new("Sound", game.InsertParentNameHere)
It is recommended to use the Workspace as the parent of the sound.
And since the sound needs an ID to work, add another line of script to give the sound the ID of choice. To find the ID of the sound you want to use, find it on the Roblox website and copy the numbers on the search bar.
game.InsertParentNameHere.SoundId = "InsertIDHere"
All you need to do after that is make the sound play. Under the line that connects the mouse click to the functions, you can type this:
game.InsertParentNameHere.Sound:Play()
You can also just make the sound without the script in the Studio and then use the :Play() function below the connection of mouse click and functions.