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

Need help with a Music Toggle Script! Using Click Detectors?

So, I have a music block with a click detector that plays music on a click, I was wondering how to toggle the music on/off. This is what I have so far.

Brick = script.Parent
Sound = Brick.Sound

function onClicked()
	Sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Any help counts, thanks!

2 answers

✓ Accepted answer

Alright! I'll help you out.

What you need to do is create a new variable called toggled and set it to false.

Brick = script.Parent
Sound = Brick.Sound
Toggled = false

function onClicked()
    Sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

After that, we need to check whether the music is playing, let's do it by checking if the variable is true or false!

Brick = script.Parent
Sound = Brick.Sound
Toggled = false

function onClicked()
    if Toggled == false then
          Toggled = true
  	  Sound:Play()
    else
	   Toggled = false
	   Sound:Stop()
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Explanation:

The toggled variable is set to either true or false so that the script can see if the audio is playing. You could also check the .IsPlaying property, however, in this example I used a variable.

Hope I helped you out!

4 comments

0

lol I have mine with just a line difference. (no flex)

0

yes, but i posted an explanation and tried to explain the script, you can tell the guy is new. and you just spedran that script like ur sonic, no offense, but i think if u answer u should give an explanation to the asker

0

Thanks for the help guys!

0

Sorry, that's just how I am and just hungry for reps. That's all. LOL im a true gangsta

Just run the opposite of 'Playing' when it's been clicked by a player using the 'not' operator.

My take:

Brick = script.Parent
Sound = Brick.Sound

function onClicked()
	Sound.TimePosition = 0
    Sound.Playing = not Sound.Playing
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 comment

0

Thanks man, this helped!

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