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

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

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!

1 comment

0

it's MouseButton1Down, not MouseButton1down because lua is case sensitive. make sure that this is a localscript inside a button too

4 answers

✓ Accepted answer

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)

1 comment

0

Thanks!

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.

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. :)

1 comment

0

idk of it works.

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