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

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

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
	


4 comments

0

Sound:Pause()?

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.

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

0

I put an answer bud.

2 answers

✓ Accepted answer

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

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