Sound plays when clicking part?

so basically i just want a sound to play when i click a part. this is what script i have but it wont work. the script is located inside a clickdetector inside a part.

script.Parent.MouseClick:connect(function() script.Parent.Parent.Sound:Play(2778386920) 
end)

8 Likes

Play() does not have any arguments for it, so just get rid of the numbers in the () and you should be good. If you’re trying to give the sound an id, change it’s SoundId property.

2 Likes

what? so i just need a sound inside the part and put the id there?

It looks like you already have a sound in the part, but yes, a sound should be in it, and its SoundId property should be the id of the sound you want to play.

3 Likes

:Play() is a function of Sound. You cannot do :Play(SoundId), instead you’ll have to create a Sound and set it’s SoundId.

local Sound = Instance.new("Sound")
Sound.SoundId = 2778386920
Sound.Parent = YourPartLocation

YourPartLocation.ClickDetector.MouseClick:Connect(function()
     Sound:Play()
end)
5 Likes

okay, i put this in a script, put the script inside a part and changed the your part location to workspace. so does the script and sound need to be inside the clickdetector or not

The Script can be anywhere you want as long as the locations are correct.
I made an example script for you that will automatically create a Part and setup everything for you:

local Part = Instance.new("Part", workspace)
Part.Anchored = true
Part.Position = Vector3.new(0, 0, 0)
-- Creating a Part in workspace, anchoring it and setting it's position to 0, 0, 0

local Sound = Instance.new("Sound", Part)
Sound.SoundId = "rbxassetid://156286438"
-- Creating a sound in Part and setting it's SoundId.

local ClickDetector = Instance.new("ClickDetector", Part)
-- Creating a ClickDetector in Part.

ClickDetector.MouseClick:Connect(function()
	Sound:Play()
end)
-- When MouseClick is fired, the Sound will be played.

Note: The script can be put anywhere you’d like.

1 Like

okay, so how do i now make sure the part is on the right place?

To check if a Part exists somewhere you can use:

if game.Workspace:FindFirstChild("Name") == nil then
     print("Does not exists.")
else
     print("Exists.")
end

well yeah but i mean, instead of creating a new part i already made a button on a special place, so what script do i need to put in there so that part makes the sound?

Try not create a new part for the sole purpose of playing a sound. It doesn’t make much sense gamewise.

Here’s the code you want to use:

script.Parent.ClickDetector.MouseClick:Connect(function()
    script.Parent.(add sound name here).Playing = true
end

Make sure the script, ClickDetector, and audio are all children of the Part.

okay then, so what are the location? part, clickdetector inside part, sound inside part and script inside part? all not in eachother?

Yes. Make it so the only arrow you see on the sidebar is the part splitting into those three assets.

(if the answer above helped, mark it as a solution so there are no further replies.)

1 Like

yes im testing it out and then if it works ill do it

okay it still doesnt work, maybe it has to do with background music i have?

Would you mind taking a screenshot of your Workspace?

image

That’s the same orientation that I’ve been using, so it should be working. Did you paste the audio ID into the audio?

Also, did you rename the (audio name here) in the script to Alarm1?

no, i put in the add sound name here the sound name… not the id

You have to define SoundId differently than just giving it inside of brackets. Here is an example:

script.Parent.MouseClick:Connect(function() 
     script.Parent.Parent.Sound.SoundId = "rbxassetid://2778386920"
     script.Parent.Parent.Sound:Play()
end)