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

Gui Turn music on and off not working. Why?

Hi! I can't seem to figure out what's wrong with this script. Can you please help me?

function MouseClick()
			script.Parent.Name = "Close"
		script.Parent.Text = "Music: On"
			if (script.Parent.Name == "Close") then
while (script.Parent.Name == "Close") do
		script.Parent.Parent.ss:Play()
		wait(120)
		script.Parent.Parent.ss:Stop()
		script.Parent.Parent.sss:Play()
		wait(71)
		script.Parent.Parent.sss:Stop()
		script.Parent.Parent.ssss:Play()
		wait(101)
		script.Parent.Parent.ssss:Stop()
		script.Parent.Parent.sssss:Play()
		wait(76)
		script.Parent.Parent.sssss:Stop()
end
	else 
		wait(1)
		script.Parent.Name = "Open"
		script.Parent.Parent.ss:Stop()
		script.Parent.Parent.sss:Stop()
		script.Parent.Parent.ssss:Stop()
		script.Parent.Parent.sssss:Stop()
		script.Parent.Text = "Music: Off"
		end
end 

script.Parent.MouseButton1Click:connect(MouseClick)

1 answer

The reason this isn't working is because when you do:

while (script.Parent.Name == "Close") do script.Parent.Parent.ss:Play() wait(120) script.Parent.Parent.ss:Stop() script.Parent.Parent.sss:Play() wait(71) script.Parent.Parent.sss:Stop() script.Parent.Parent.ssss:Play() wait(101) script.Parent.Parent.ssss:Stop() script.Parent.Parent.sssss:Play() wait(76) script.Parent.Parent.sssss:Stop() end

the loop is working down the script playing each song and waiting. So a better way to do this would be to use a boolean if you don't already know thats a true or false value.

Instead of changing the name of it to close and open you can make a new boolean value.

playing = false

to check that the player clicks the button you can make a function like you already did.

playing = false

script.Parent.MouseButton1Down:connect(function()
	if playing = false then -- check if audio is playing 
		playing = true -- allow the script to know if its on or not
		script.Parent.Text = "Music: On" -- change the name like you wanted
		script.Parent.Parent.ss:Play() -- play all the music
     		wait(120)
        	script.Parent.Parent.ss:Stop()
        	script.Parent.Parent.sss:Play()
       	 	wait(71)
       	 	script.Parent.Parent.sss:Stop()
       		 script.Parent.Parent.ssss:Play()
       		 wait(101)
     		 script.Parent.Parent.ssss:Stop()
        	script.Parent.Parent.sssss:Play()
        	wait(76)
        	script.Parent.Parent.sssss:Stop()
	else -- if audio is playing we want to stop it
		playing = false
        	script.Parent.Text = "Music: Off"
     		script.Parent.Parent.ss:Stop()
       		script.Parent.Parent.sss:Stop()
        	script.Parent.Parent.ssss:Stop()
        	script.Parent.Parent.sssss:Stop()
	end
end)

Also you may want to try not to use a repetitive letter for naming your sounds as it can get confusing.

I hoped this helps and it works as I did not test it.

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