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

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

Asked by 6 years ago

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)

0
post the code you got, and I shall deliver. Decimaprism 65 — 6y
0
The code (I assume you mean ID) is located at the end of my question. SlingbladePlayz 0 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
Ad
Log in to vote
0
Answered by 6 years ago

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)
Log in to vote
0
Answered by 6 years ago

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?

0
Can you show us a picture of what you got please? Decimaprism 65 — 6y

Answer this question