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

What ways can I make a sound stop when certain conditions aren't met?

Example Attempt #1

while true do
	wait()
	if O.Value == true or O2.Value == true then
		M:Play()
		wait(M.TimeLength)
	else
		M:Stop()
	end
end

I apologize for any lack of detail in my explanation. I've been scripting/coding/programming a sink (and other things) for several hours trying to intergrate different things and I'm a little worn.

My Problem

So what I'm working on right now is a sink, and while everything works completely fine I decided to add in a sound effect of running water. It works as I want it to like it is, but I realized if I were to turn off the sinks water the sound would play until the sound is completely done and then it stops ( and yes, I do know the reason behind that is because I have it wait the time of the sound, but this is the only example I could provide of what I've tried.) I have two variables, O and O2 that determine if either of the faucets are on, and when you click a button ( equivalent of turning sink knob ) the water runs and the sink is considered 'on.'

But what I can't seem to figure out is how would I stop the sound from playing instantly once both sinks are off( because the sound will still play as long as one is on as intended ).

Question

So in short, how would I stop the music/sound stop playing the minute both variable values == false? Am I even on the right track or is this a bad approach?

Thanks for the help!

2 answers

✓ Accepted answer

You can use the and keyword combined with an elseif statement to provide a secondary custom conditional the requires both flags to be True.

while wait() do
    if (O.Value == true or O2.Value == true) then
        if not (M.Playing) then 
            M:Play()
        end
    elseif (O.Value == false and O2.Value == false)
        M:Stop()
    end
end

6 comments

0

Puts me in the same hole. After I turn off both faucets the sound plays out until it's done then it stops. I'm trying to have it stop as soon as the condtions aren't met, which is when O and O2 = false. Maybe my sound is too long?

0

Oh, I see what the problem is

0

You’re :wait() line is yielding, making the code stop until it’s finished so it’ll never make it to the elseif statement

0

Try this?:

0

I edited my answer.

0

You genius. Thanks a lot.

I don't actually get it, but I think this might help you? Could be this:

if O.Value == true or O2.Value == false then -- if ONE of these variables = false then
--run code
end

Or this:

if O.Value == true and O2.Value == false -- with the "and", the if statement will only run if BOTH of these are false
--run code
end

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