Ambience not turning on when GUI collapses

This is server script :

local sound = game.SoundService.LobbyAmbience

game:GetService("ContentProvider"):PreloadAsync({sound})
game.ReplicatedStorage.RemoteEventPressAnyKeyToStartTheAmbience.OnServerEvent:Connect(function(player)
	sound:Play()
end)

This is localscript :

local GUI = script.Parent
local userinputservice = game:GetService("UserInputService")
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEventPressAnyKeyToStartTheAmbience")

userinputservice.InputBegan:Connect(function(input, gameProcessedEvent)


	if input.UserInputType == Enum.UserInputType.Keyboard then
		print("Player has pressed keyboard")
		Remote:FireServer()

	end





end)

So there are two identical local scripts like that but they both are connected to different server scripts. One does the GUI collapse when any button has been pressed and the other one when a button has been pressed, turns on the ambience. GUI collapses all right with everything working except the ambience is not playing and there are no errors or warnings (yes I checked my volume too).

Try to change your server script with this?

local debounce = false
local sound = game.SoundService.LobbyAmbience

game:GetService("ContentProvider"):PreloadAsync({sound})
game.ReplicatedStorage.RemoteEventPressAnyKeyToStartTheAmbience.OnServerEvent:Connect(function(player)
	if debounce == false then
		debounce = true
		sound:Play()
		sound.Ended:Wait()
		debounce = false
	end
end)

Still nothing works. And there are no errors and warnings.

Try to add prints to check what part isnt working

local debounce = false
local sound = game.SoundService.LobbyAmbience

game:GetService("ContentProvider"):PreloadAsync({sound})

print(1)
game.ReplicatedStorage.RemoteEventPressAnyKeyToStartTheAmbience.OnServerEvent:Connect(function(player)
	if debounce == false then
		debounce = true
		sound:Play()
		print(2)
		sound.Ended:Wait()
		debounce = false
	end
end)
local GUI = script.Parent
local userinputservice = game:GetService("UserInputService")
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEventPressAnyKeyToStartTheAmbience")
print(3)
userinputservice.InputBegan:Connect(function(input, gameProcessedEvent)

	print(4)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		print("Player has pressed keyboard")
		Remote:FireServer()

	end





end)

tell me if all of them are printing (1, 2, 3, 4)

So I added the print commands and the only number that ever printed was “1”.

Where is the local script located? Is it in StarterGui?

The GUI collapse localscript is in there. But the ambience local script is in soundservice since I also had no idea where else to put it.

Move it to StarterGui then? Maybe that’ll work.

You have to put localscripts in StarterGui since it wont work in SoundService

I did it and this time it printed out the following :

1
3
4
4
4
2
and now it just goes 4 repeadly

So does the ambience turn on now?

No it just prints out the messages but there’s still no sound.

Ahhh, I just noticed your LobbyAmbience is in SoundService. Place this into the Workspace and change the 1st line of your script to this

local sound = game.Workspace.LobbyAmbience

Again it just prints out 1 and nothing else happens.

There is no need for you to play the sound on server, and by the time the player will press a key sound should already load, so why not move everything over to client and use SoundService:PlayLocalSound() instead?

Its supposed to still print 2,3,4 since I only told you to change the LobbyAmbience and the ServerScript, both of which doesnt affect the localscript. Unless you undid your changes to the localscript? Make sure your localscript is in StarterGui, and the LobbyAmbience is in the Workspace.

1 Like

Thank you for clarifying it since when I did that now it works.

1 Like