Sound playing even when value is set to 0

I’m making a menu GUI and of course, as most main menus do, mine plays background music. The problem is that even when the value (what I am using to control whether the music plays or not) is set to 0, the music continues to play. I know it’s probably a dumb question, but I am not a very good scripter.

Here’s the script:

local music = script.Parent.MenuMusic
local TS = game:GetService("TweenService")

if script.Parent.Value == 1 then
    music:Play()
elseif script.Parent.Value == 0 then
    local Tween = TS:Create(script.Parent.MenuMusic, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 0})
    Tween:Play()
    Tween.Completed:Wait()
    music:Stop()
end
local music = script.Parent.MenuMusic
local TS = game:GetService("TweenService")

script.Parent.Changed:Connect(function()
   if script.Parent.Value == 1 then
       music:Play()
   elseif script.Parent.Value == 0 then
       local Tween = TS:Create(script.Parent.MenuMusic, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 0})
       Tween:Play()
       Tween.Completed:Wait()
       music:Stop()
   end
end)

Hmm…I tried this and now the music doesn’t play at all.

local music = script.Parent.MenuMusic
local TS = game:GetService("TweenService")

function Toggle()
   if script.Parent.Value == 1 then
       music:Play()
   elseif script.Parent.Value == 0 then
       local Tween = TS:Create(script.Parent.MenuMusic, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 0})
       Tween:Play()
       Tween.Completed:Wait()
       music:Stop()
   end
end

Toggle()
script.Parent.Changed:Connect(Toggle)

This version isn’t working either.

I am testing in Roblox studio.

And it also doesn’t work in-game.

Adding on to what @SelfDeclared said! You could change the value when playing & pausing/stopping the sound! :happy2:

local music = script.Parent.MenuMusic
local TS = game:GetService("TweenService")

function Toggle()
   if script.Parent.Value == 1 then
       music:Play()
       script.Parent.Value = 0
   elseif script.Parent.Value == 0 then
       script.Parent.Value = 1
       local Tween = TS:Create(script.Parent.MenuMusic, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 0})
       Tween:Play()
       Tween.Completed:Wait()
       music:Stop()
   end
end

script.Parent.Changed:Connect(Toggle)

Since I’m assuming you’re using an instance as the value, you would also have to add another .Value in order to actually get the value.

local music = script.Parent.MenuMusic
local TS = game:GetService("TweenService")

function music()
	if script.Parent.Value.Value == 1 then
		music:Play()
	elseif script.Parent.Value.Value == 0 then
		music:Stop()
		local Tween = TS:Create(script.Parent.MenuMusic, TweenInfo.new(1, Enum.EasingStyle.Linear), {Volume = 0})
		Tween:Play()
		Tween.Completed:Wait()
	end
end
script.Parent.Value:GetPropertyChangedSignal("Value"):Connect(music)
1 Like

This will work if your layout is like this
(you should also change the name for value in future projects so you don’t get confused.)
image

this wasn’t working for me either, do you think there’s maybe I need to change a setting on the audio?

Show me your explorer window so I can make a script that actually fits.

What parts do you want to see? (as in for example, Lighting)

I would want to see the model with the script of course. Thought that was obvious.