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

Sound doesn't stop when pressing a GUI button?

I know this might sound silly, since it's such a simple script. But I was trying to create "Toggle Radio" like After the Flash, and I thought this code would work, but it doesn't stop the sound or show any errors in the Developer Console. Here's my code:

local Sound1 = script.Parent.TimeAndTimeAgain -- References the sound

script.Parent.MouseButton1Down:Connect(function() -- Detects when clicked, if so commences these events
	if Sound1.Playing == false then -- If the sound isn't playing, it plays the sound
		Sound1:Play()
	else
		Sound1:Stop() -- If the sound is playing, it stops the sound
	end
end)

Thanks!

5 comments

0

Try if Sound1.IsPlaying == false then

0

Doesn't work

0

either your asset ID is invalid or your sound isn't called 'TimeAndTimeAgain' because there's no problems with the code, an invalid AssetID is most likely the problem.

0

Nope, the asset ID isn't invalid and it isn't showing errors. The sound is named TimeAndTimeAgain. It's in the script

0

script's parent.

2 answers

✓ Accepted answer

Try this

local Sound1 = Instance.new("Sound")
Sound1.SoundId = "rbxassetid://1837324424" 
Sound1.Parent = script.Parent

script.Parent.MouseButton1Down:Connect(function() -- Detects when clicked, if so commences these events
	if Sound1.IsPlaying == false then -- If the sound isn't playing, it plays the sound
		Sound1:Play()
	else
		Sound1:Stop() -- If the sound is playing, it stops the sound
	end
end)

5 comments

0

I don't know why it doesn't work on mine, it might be playing multiple times. But I don't get how that's possible because I'm only clicking it once.

0

Is it supposed to be a local script? Because on mine it is.

0

Yeah its supposed to be in a local script. Is this 100% the code or is this a snippit of one? This code isn't set in a loop?

0

Nope.

0

This is all of the code.

Apparently, the Play and Stop() function just looped??? I'm so confused about how it was looping. But anyway, I got it fixed. Thanks for your help!

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