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

How do I stop a sound after a ClickDetector plays the sound?

I am working on making a tv show for my model TV in one of my games. What I want to do is that if you click the block again, it turns off the sound and when you click it, then it would play the sound again. I could not find any videos. Code is below.

Brick = script.Parent
Sound = Brick.Show

function onClicked()
	Sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 answer

Here's my way to do this, as I don't like using functions that way

local sound = script.Parent.Show
local a = 1

script.Parent.ClickDetector.MouseClick:Connect(function()
	if a == 1 then
		sound:Play()
		a = 2
	elseif a== 2 then
		sound:Stop()
		a = 1
	end
end

local sound = script.Parent.Show
local bool = false

script.Parent.ClickDetector.MouseClick:Connect(function()
	bool = not bool

	if not bool then
		sound:Play()
	else
		sound:Stop()
	end
end

2 comments

0

Um why use number instead of Boolean?

0

You could also, this was just a better example if you're more on the new end.

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