Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can someone help me with coding an in-game button to play sounds?

I have a keypad door all coded. But I really want to put a sound effect in so whenever I press one of the buttons it plays that sound. Can someone please help me? (It would be extra-helpful if the script you create has the sound ID already in where it needs to go. Here is the ID: 472556995)

2 comments

0

post the code you got, and I shall deliver.

0

The code (I assume you mean ID) is located at the end of my question.

3 answers

This assumes that you have your sounds in the ScreenGui object and then the Button is apart of Frame and is named "Button"

Also, I think there is a million ways to do this as well so yep...

local UI = script.Parent
local Button = UI.Frame.Button

local Sound1 = UI.Sound1
local Sound2 = UI.Sound2
local Sound3 = UI.Sound3
local Sound4 = UI.Sound4

UI.Button.MouseButton1Click:connect(function()

Sound1:Play()
Sound2:Play()
Sound3:Play()
Sound4:Play()

end)

or

for randomized sound

local UI = script.Parent
local Button = UI.Frame.Button

local Sound1 = UI.Sound1
local Sound2 = UI.Sound2
local Sound3 = UI.Sound3
local Sound4 = UI.Sound4

UI.Button.MouseButton1Click:connect(function()

UI:FindFirstChild("Sound".. math.random(4)):Play()

end)

Step 1: Place a ClickDetector inside the button. Step 2: Place a Script inside the ClickDetector. Step 3: Place a Sound inside the button.

Okay, now that that's out of the way, we need to script it. So, whenever the ClickDetector fires the function, MouseButton1Click, it will play the sound. However the sound needs to have a SoundID assigned which is that id you have.

Here's a code example.

local clickDetector = script.Parent
local sound = script.Parent.Parent.Sound

clickDetector.MouseButton1Click:connect(function()
	sound:Play()
end)

So, I have assigned the script, And I have put the sound ID into the button. Is there anything else I need to change? Also, if I change the name of the sound, do I need to edit the script to detect that?

1 comment

0

Can you show us a picture of what you got please?

This discussion is preserved as it was. New answers, comments, and votes are closed.