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 6 years ago

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

enabled = false
b = script.Parent

b.MouseButton1down:connect(function()
        if enabled then
        game.SoundService.Dramatic:Play()
        enabled = false
            else
        game.SoundService.Dramatic:Stop()
        enabled = true
        end
end)

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 — 6y

4 answers

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

No need for variables.

local button = script.Parent

function SET_SOUND()
    local sound = game.SoundService.Dramatic
    if (not sound.Playing) then 
        sound:Play()

    elseif (sound.Playing) then 
        sound:Stop()
    end
end

button.MouseButton1Down:connect(SET_SOUND)
0
Thanks! CaptainAlien132 225 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

use mouse.Button1Click event

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

local musicIsPlaying = false
b = script.Parent

b.MouseButton1down:Connect(function()

    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.]]--

        game.SoundService.Dramatic:Play()
        musicIsPlaying = true

    else

        game.SoundService.Dramatic:Stop()
        musicIsPlaying = false

    end
end)

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 6 years ago
Edited 6 years ago

This may help.

While true() do
local b = script.Parent
b.enabled = false

b.MouseButton1down:Connect(function()
        if b.enabled = true then
        game.SoundService.Dramatic:Play()
        b.enabled = false
            else
        game.SoundService.Dramatic:Stop()
        b.enabled = true
        end
end)

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

0
idk of it works. ILikeTofuuJoe 1 — 6y

Answer this question