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

How do I make a GUI button that when pressed plays a sound?

Asked by 7 years ago

I'm trying to make a GUI that plays some dramatic music when pressed, and when pressed again it disables.

01enabled = false
02b = script.Parent
03 
04b.MouseButton1down:connect(function()
05        if enabled then
06        game.SoundService.Dramatic:Play()
07        enabled = false
08            else
09        game.SoundService.Dramatic:Stop()
10        enabled = true
11        end
12end)

Thanks if you can help, I'm also a bit of a noob so please go easy!

0
it's MouseButton1Down, not MouseButton1down because lua is case sensitive. make sure that this is a localscript inside a button too brokenVectors 525 — 7y

4 answers

Log in to vote
1
Answered by
hellmatic 1523 Moderation Voter
7 years ago

No need for variables.

01local button = script.Parent
02 
03function SET_SOUND()
04    local sound = game.SoundService.Dramatic
05    if (not sound.Playing) then
06        sound:Play()
07 
08    elseif (sound.Playing) then
09        sound:Stop()
10    end
11end
12 
13button.MouseButton1Down:connect(SET_SOUND)
0
Thanks! CaptainAlien132 225 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

use mouse.Button1Click event

Log in to vote
0
Answered by 7 years ago

I would use a variable named musicIsPlaying instead of enable because that might help think about the problem better. Then in the script I would have a function that works like yours but a little bit different. Here is an example:

01local musicIsPlaying = false
02b = script.Parent
03 
04b.MouseButton1down:Connect(function()
05 
06    if not musicIsPlaying then -- [[this is where you probably had a problem. You were going to stop the music first instead of play because the first value of your enable variable was false and it wouldn't have made it past this if statement.]]--
07 
08        game.SoundService.Dramatic:Play()
09        musicIsPlaying = true
10 
11    else
12 
13        game.SoundService.Dramatic:Stop()
14        musicIsPlaying = false
15 
16    end
17end)

I always recommend using variable and function names that make sense and help you understand what you are dealing with. Hope this helps you. Have a great day scripting.

Log in to vote
-1
Answered by 7 years ago
Edited 7 years ago

This may help.

01While true() do
02local b = script.Parent
03b.enabled = false
04 
05b.MouseButton1down:Connect(function()
06        if b.enabled = true then
07        game.SoundService.Dramatic:Play()
08        b.enabled = false
09            else
10        game.SoundService.Dramatic:Stop()
11        b.enabled = true
12        end
13end)

I'm a noob so IDK if you should use while true() do. :)

0
idk of it works. ILikeTofuuJoe 1 — 7y

Answer this question