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

How do I pause and unpause music with every other click?

Asked by 5 years ago
Edited 5 years ago

I'm trying to make it an "on" and "off" switch just by clicking it again and I can't get it right. When I click it the first time it starts, the second time it SHOULD pause, but it doesn't.

local Yes = 1

local function onclick (click)
    if Yes == 1 then
        script.parent.SweetJams:Play()
    end 
    Yes = 2
end

if script.Parent.ClickDetector.MouseClick:Connect(onclick)and Yes == 2 then
    script.parent.SweetJams:Stop()

end



0
`Sound:Pause()`? TheeDeathCaster 2368 — 5y
0
You set "Yes" to 2, and you check if its 1 or 2, but you never set it to 1, except when you set the variable. DinozCreates 1070 — 5y
0
next time put it in the answer box since you didn't just copy/paste code to help, but you showed me an error that I then fixed. You didn't ONLY help. Thanks :D popdrinkman 8 — 5y
0
I put an answer bud. DinozCreates 1070 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Simplified this down. Its much easier to track what you're doing if you use a variable that makes sense in the given situation. If MusicPlaying is true, the music should be playing, right?

local MusicPlaying = false

function onclick (click)
    if MusicPlaying then
        script.parent.SweetJams:Pause()
        MusicPlaying = false
    else
        script.parent.SweetJams:Play()
        MusicPlaying = true
    end
end

script.Parent.ClickDetector.MouseClick:Connect(onclick)
Ad
Log in to vote
0
Answered by 5 years ago

You would need to add a toggle (boolean preferably) like so:

local toggle = true;
if toggle then
    music:Play();
    toggle = false;
else
    music:Stop();
    toggle = true;
end

Answer this question