Why does my LocalScript not change the BoolValue?

I have this code:

local SoundService = game:GetService("SoundService")
local sound = SoundService.BackgroundMusic

script.Parent.MouseButton1Click:Connect(function()
	local m = sound.Muted
	local muted = m.Value
	if muted == false then
		muted = true
		sound.Volume = 0
	elseif muted == true then
		muted = false
		sound.Volume = 0.075
	end
end)

When the game starts the BoolValue is false. And when I click the button it successfully changes the sound’s volume to 0, but does not changed the BoolValue, and so when I click it again it does not set the sounds volume to 0.075.

I also tried with ‘muted = blah’ being ‘m = blah’ and that did not work

Thank you for any help in advance

This is a pass by reference and pass by value sort of problem.

Essentially muted is set to the VALUE of m not the reference to the value of m.

To change the value you need to do m.Value = true; m.Value = false

1 Like

‘Muted’ is ‘m.Value’, so why would that not be working the same?

‘Muted’ is the value of m, not the refrence to m.Value

It’s being set to whatever m.Value is at that time.

1 Like

I see what you mean… thank you. Variables confuse me

1 Like

I also had a similar problem while I was making some powers for my game.
I made a remote event that handles changes in values so it happens across the server.

game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):OnServerEvent:Connect(function(value)
Game:GetService("SoundService").BackgroundMusic.Muted.Value = value
end

And in the script where you want to change it just do

game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireServer(true) -- true or false whatever you want to change it to

I don’t think I need to say this but you can just make a variable for all that gamegetservice stuff so its shorter.
This did work for me. I’m not sure if it would work for you but lets see

To help with that:

When a value is being assigned to a variable it is either:

Pass by reference: The variable is given a pointer to the value, not the value itself. This essentially means when it is changed anywhere, any other variables with the pointer will also have that change.

Pass by value: The variable is given a copy of the value.

Tables are always passed by reference (unless it’s passed over the network), everything else is passed by value.

Under the hood all objects are tables which is why you can store the reference to sound.Muted, but not sound.Muted.Value

So I think I’m also having the same problem here, but i tried changing things around and I still wasnt able to figure out what the problem is.

local SoundService = game:GetService("SoundService")
local sound = SoundService.BackgroundMusic
local sounds = {1837719490, 9042454095, 9044702906}
local played = 123

while true do
	
	local soundToPlay = sounds[math.random(1, #sounds)]
	print (soundToPlay)
	if soundToPlay ~= played then
			played = soundToPlay
			sound.SoundId = 'rbxassetid://' .. tostring(soundToPlay)
			sound:Play()
			wait(sound.TimeLength)
			sound:Stop()
			sound.TimePosition = 0
		end
	wait(.1)
end

When the game starts it just repeatedly skips the ‘if’ event and forever picks out new song ids.
I was hoping you might see what the problem here is while I cannot… is it a problem with the variables?

Thank you in advance. I only recently started trying to script

This one is a different issue, the information for the sound hasn’t been loaded in yet, which means sound.TimeLength is 0 until it’s loaded in.

Just throw in a

if not sound.IsLoaded then 
	sound.Loaded:Wait()
end

right after setting the SoundId.

You’re doing great :+1:

1 Like