Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
djwchon 10
4 years ago

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

Log in to vote
0
Answered by
Mroczusek 111
4 years ago

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!

0
lol I have mine with just a line difference. (no flex) DiamondComplex 285 — 4y
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 Mroczusek 111 — 4y
0
Thanks for the help guys! djwchon 10 — 4y
0
Sorry, that's just how I am and just hungry for reps. That's all. LOL im a true gangsta DiamondComplex 285 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

0
Thanks man, this helped! djwchon 10 — 4y

Answer this question