How to play a sound when value is On

Hello,

I’m trying to make a sound play when a value is on.
I have 2 values - If you touch it turns on/off.
I have another value - if the vehicle is stopped. (Which works.)

So i have a script, when you touch a part, the value turns on.
And i have another script, if you touch a part the value turns off.

Now what i need help with, if the value is turned on, and the value of the vehicle is stopped is also on, a sound plays.
BUT - If the value is on, but the vehicle stopped value is not on, and you touch the value to turn the value off, the sound cannot play anymore.

I just don’t understand how to do things while values turn on mid game.

If you wan’t me to share any scripts, i can only share in private.

I can help you figure out the logic and, even without accessing your exact scripts, I can try to provide a generalized idea of what your script should look like.

Most probably, you want to have a listener checking for the changes of values, right? Whenever a value changes, it’s good to use Roblox’s in-built .Changed event for that. If both values are of “BoolValue”, it can be like this:

--Let's assume TouchValue is on/off value when you touch something.
--And, VehicleStoppedValue is the value when vehicle stops.

--Adding listener for touch value
TouchValue.Changed:Connect(function(newValue)

    --Checking if both values are true (Touched and Vehicle Stopped)
    if TouchValue.Value == true and VehicleStoppedValue.Value == true then
        --If true, play sound here
    elseif TouchValue.Value == false then
        --If touch value is turned off, Stop the sound
        --This part effectively means no matter what the value of VehicleStoppedValue, sound should stop if touch value is off
    end
end)

--Adding listener for VehicleStoppedValue, you may not need this.
--Including in case if vehicle stops after the touch, and you want to play sound then as well. 
VehicleStoppedValue.Changed:Connect(function(newValue)
    if TouchValue.Value == true and VehicleStoppedValue.Value == true then
        --If true, play sound here
    end
end)

IMPORTANT: Replace TouchValue and VehicleStoppedValue with actual variables that represent these values, also make sure to replace --If true, play sound here and --If touch value is turned off, Stop the sound with your actual sound execution code.