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!
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!
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)