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)
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)
Adding on to what @SelfDeclared said! You could change the value when playing & pausing/stopping the sound!
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)