Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this music playing script that I made?

Asked by
Fewice 22
5 years ago
local Volume = game.Workspace.Sound.Volume
script.Parent.MouseButton1Click:connect(function()
    if Volume = 0.2 then 
        Volume = 0
    elseif Volume = 0 then
        Volume = 0.2
        end
end)

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The problem lies in the fact that you wrongly assume that the Volume variable is a reference to the actual property of the Sound. If the volume of the Sound changes, the variables you have defined won't update; only its Volume property will change. In order to fix this, you can remove the Volume variable and only make direct accesses to the Volume property of the Sound. You could also modify the variables to hold the Sound object itself.

local Sound = game.Workspace.Sound

script.Parent.MouseButton1Click:Connect(function() -- :connect is deprecated, use :Connect
    if Sound.Volume == 0.2 then 
        Sound.Volume = 0
    elseif Sound.Volume == 0 then
        Sound.Volume = 0.2
    end
end)


And as previously mentioned by brokenrares, you need double equals in conditionals.

0
Thanks, also I was wondering what the "Sound.---" would be for stopping a sound (so Sound.Play() to play, what for stop?) Fewice 22 — 5y
0
Hi Fewice the function is Sound:Stop() User#23407 5 — 5y
0
Hm, I thought it was that but it didnt work haha, ty. Fewice 22 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Basically you're not using the if statement in the right way. You're checking if the Volume is 0.2 but you'll have to use double '=' for that since it's inside an if statement So:

local Volume = game.Workspace.Sound.Volume
script.Parent.MouseButton1Click:connect(function()
    if Volume == 0.2 then 
        Volume = 0
    elseif Volume == 0 then
        Volume = 0.2
    end
end)

0
Thanks man. Fewice 22 — 5y

Answer this question