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

Music won't stop playing?

Asked by
Paldi 109
9 years ago

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)
0
It might be because you made "on" local. Remove Local and then try it. alphawolvess 1784 — 9y
0
still not stopping it Paldi 109 — 9y
0
This should work if you removed local, I don't see why it wouldn't. Any other scripts screwing with anything? alphawolvess 1784 — 9y
0
theres no other scripts in this gui Paldi 109 — 9y
View all comments (4 more)
0
now its restarting it when i click again Paldi 109 — 9y
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 . alphawolvess 1784 — 9y
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. Perci1 4988 — 9y
0
when i click the button the music start playing and when i click it again instead of stopping it does nothing Paldi 109 — 9y

1 answer

Log in to vote
3
Answered by
Wizzy011 245 Moderation Voter
9 years ago

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.

Ad

Answer this question