Sound not pausing in local script

Hello, so I’m trying to make a sound pause locally when a server-changed boolValue turns true.
Here’s the function:

genOn.Changed:Connect(function(NewValue)
	if NewValue and UI.Enabled == true then
		workspace.Music["SceneMusic"..cameraVal.Value]:Play()
		musicBG:Pause()
		print("Generator is on and you are spectating")
	else
		if UI.Enabled == true then
			duckMusic:Stop()
			teddyMusic:Stop()
			print("Generator is off and you are spectating")
		end
	end
end)

Everything inside the function works as intended, it prints all it has to print and plays the “SceneMusic” as it should, but when it comes to pausing MusicBG it just doesn’t. I have tried Stopping it, but that doesn’t work either, the only way I can stop the sound from playing (Only locally) Is by destroying it, but I need to play it again after something else happens.

You could try setting the Volume of the sound to 0. Be mindful of the fact that this value defaults to 0.5, you might make it louder than intended if you set it higher when turning the sound back on.

It wouldn’t pause the sound though

I am not sure what could be causing this since you didnt show more about the musicBG value.

But one thing I can say is you are using .Changed wrong. It does not pass NewValue, but rather the Property name that was changed.

For example:

something.Changed:Connect(function(property)
    if property == "Name" then
        print("The name was changed to: ", something.Name)
    end
end)

Also, if you only want to listen to one property only, you can use:
https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal
(which I recommend using over .Changed)

I didn’t put more information of musicBG because it’s basically called 20 times in a script long 500 lines and I thought it’d be confusing and pointless, although if it’s really necessary I can send the whole script I guess. Thanks for pointing out that I used .Changed wrong.

There is your problem. You can’t use :Pause() or :Stop() or :Play() on a BoolValue.

Those calls are for Sound objects.

oh geez I’m so sorry I meant GenOn, musicBG is a sound object. Haven’t slept in a while got confused hehe :sweat_smile:

Ah, yea that happens sometimes lol.

Try this and see if it works:

genOn.Changed:Connect(function()
	if genOn.Value and UI.Enabled then
		workspace.Music["SceneMusic"..cameraVal.Value]:Play()
		musicBG:Pause()
		print("Generator is on and you are spectating")
	elseif UI.Enabled then
		duckMusic:Stop()
		teddyMusic:Stop()
		print("Generator is off and you are spectating")
	end
end)

Unfortunately, it still doesn’t Pause, which makes musicBG and SceneMusic overlap, creating a very annoying sound :frowning:

Where is musicBG located (parented to) and is this a LocalScript or normal Script? And where is this script located also?

In the same folder as SceneMusic, I checked the local and it’s correct, it also pauses correctly in other functions that use musicBG as well. The script is a local script as stated in the title

LocalScripts dont run in Workspace.

It’s not in the workspace, it’s in a GUI.
I will try to explain better what the scripts do, should have done that when creating the topic, give me a couple of minutes : P

Ok you confused me. You said the script was in the same folder as SceneMusic and you reference SceneMusic in workspace.Music → workspace.Music["SceneMusic"..cameraVal.Value]:Play()

EDIT: wait my bad, you were talking about musicBG.

no, MusicGB is in the folder workspace.Music, the script itself is a GUI

Yes I guess you aren’t the only one who is tired lol.

1 Like

So, Basically, the local script allows you to change the camera with the tweenservice while you’re ‘Spectating’, it also lets you exit the ‘Spectating’ Status. Another part of the game which is important to know is that you can turn on and off an electricity generator, which stops or plays all sounds, actions, lights etc. etc.
I’ve made it so that when you spectate a specific ‘Scene’ it has its own music, i.e. it stops the background music and plays another specific sound.

Like Shown Here

Imgur: The magic of the Internet

Here's also the generator in the workings

Imgur: The magic of the Internet

Now, When a player is spectating, and another player turns off the generator, everything is fine, but the problem occurs when the other player turns on the generator again, which starts the music from a server script (actually a module which is called from a server script, the server script is the generator’s script).
So I figured I’d pause the music as soon as the local script detects that the generator is on again, but unfortunately that doesn’t work! :C

So I assume because the other player starts the music (it plays on server) you can’t use :Pause() on client because Server has priority, maybe. Perhaps the only solution is to just change the volume to 0 locally so the local player wont hear it.

Also I like the game’s art direction. Gives me wonderland / fun house vibes :smile:

1 Like

I don’t know, it’s like it works when it wants, because for example when you spectate, it pauses the music which was previously played server-side. Also I think that changing the volume is an effective solution but not kind of solution that I would like to have :frowning:

Thanks for the compliments on the art direction, I really like the fun house concept :smiley:
Although all of this project is purely to improve on my scripting skills and I will probably forget it even exists when I’m satisfied with it xD

Perhaps creating all sounds client sided, having the server not control sounds like this. But rather having the server fire a remote event to the client(s) to Play/Pause/Stop that way the client can determine if it should play/pause/stop depending on certain situations like spectating. I know its not good to trust the client but in this case its fine because its just audio.