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

Music won't stop playing?

I made this script in a textbutton, the music play but wont stop playing when i click the button again. I dont get any errors

function OnClicked()
local on = false
    if on == false then
	    script.Parent.Parent.Sound:Play()
	    on = true
	    script.Parent.Text = "Stop music"
    elseif on == true then
	    script.Parent.Parent.Sound:Stop()
	    on = false
	    script.Parent.Text = "Play music"
    end
end

script.Parent.MouseButton1Click:connect(OnClicked)

8 comments

0

It might be because you made "on" local. Remove Local and then try it.

0

still not stopping it

0

This should work if you removed local, I don't see why it wouldn't. Any other scripts screwing with anything?

0

theres no other scripts in this gui

0

now its restarting it when i click again

0

I'd have to check your game or something because the script appears to be fine. Music plays when you click or do you spawn in and it's already playing? This is a local script? If not make it one .

0

All 'local' does is confine a variable to the scope it was created in. In this script, it's never used outside of the function scope, so the word 'local' is not a problem. Wizzy's answer is correct.

0

when i click the button the music start playing and when i click it again instead of stopping it does nothing

1 answer

You need to put the local on = false outside of the OnClicked function, whilst it's inside, you're resetting the value of on to false every time you click it. Moving it above line 1 will stop this from happening, and it'll remain constant.

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