I'm trying to make a GUI button that stops music, but when I press it nothing happens. This is my code:

local music = ('musicplay1')
db = true
script Parent.MouseButton1ClickConnect(function()
	if db == true and music then
		db = false
		script.Parent.Text = ("Music Off")
		game.Workspace.Audio.musicplay1:Pause()
	else
		db = true
		script.Parent.Text = ("Music On")
		game.Workspace.Audio.musicplay1:Resume()
	end
end)

Can someone help me?

5 comments

0

Open your output. The window is a vital component to programming; your code has a syntax error on line 3, you would've been informed—the Linter would've caught it too, you should see an underline in your editor.

0

You have zero* need to wrap your Strings in parenthesis, unless you wish to call methods on them directly.

0

Your 'music' variable on line 1 is redundant and has no significance to the program whatsoever, I assume you meant to check whether 'musicplay1' was a member of workspace. To do so, allocate through :FindFirstChild() and continue to affirm the Instance with your conditional statement.

0

You're referencing the Script's Parent and 'musicplay1' frequently, use a variable—for 'musicplay1' as mentioned above, you can use the variable allocating the Instance, after confirming its existence of course. Also ensure you're using meaningful identifiers, for example, 'db' is a poor naming choice (consider "Debounce" or better yet, "Toggle")

0

There is a global titled 'workspace' that more efficiently points to 'game.Workspace'.