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

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

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

✓ Accepted answer

###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.

3 comments

0

Thanks, also I was wondering what the "Sound.---" would be for stopping a sound (so Sound.Play() to play, what for stop?)

0

Hi Fewice the function is Sound:Stop()

0

Hm, I thought it was that but it didnt work haha, ty.

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)

1 comment

0

Thanks man.

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