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

The audio is looped but won't stop when I need it to. Anyone know how to fix it?

Asked by 5 years ago

Basically I want this audio to be looped but when I click the button again, it stops. It isn't happening. Anybody know how to fix this?

1script.Parent.MouseClick:connect (function(start)
2script.Parent.Parent.Audio:Play()
3script.Parent.MouseClick:connect (function(stop)
4script.Parent.Parent.Audio:Stop()
5end)
6end)

2 answers

Log in to vote
2
Answered by 5 years ago

You're repeating the same event inside of the first. There's no reason to do this.

We can put just one event, and we can just do a check inside of it to see if the audio is playing, if it isn't then we play, but if it is we stop the audio.

1script.Parent.MouseClick:Connect(function(start)
2    local audio = script.Parent.Parent.Audio --//A variable to put our audio in so it's easier to reference it
3 
4    if (audio.IsPlaying == false) then --//It isn't playing so we start it
5        audio:Play()
6    elseif (audio.IsPlaying == true) then --//It is playing so we stop it
7        audio:Stop()
8    end
9end)
Ad
Log in to vote
0
Answered by 5 years ago

This is what I personally use for my buttons. It sets the text of the TextButton to say "Music: On" or "Music: Off"

01local button = script.Parent
02 
03button.MouseButton1Click:Connect(function()
04    if button.Text == "Music: On" then
05game.Workspace.music.Playing = false
06        button.Text = "Music: Off"
07    else
08        game.Workspace.music.Playing = true
09 
10        button.Text = "Music: On"
11 
12    end
13end)

Hope this helped! :)

Answer this question