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