Playing Sounds To Specific Players

So in this case it’s client to server to client?

Yes, as you are using the GuiButton.MouseButton1Click event.

1 Like

I’m confused on how to actually code this. SO far I have:

for i,Player in pairs(game.Players:GetChildren()) do
	if Player.PlayerGui.Radio then
		
	end
end

Yes, you can do it like that but do not use Players:GetChildren(), use Players:GetPlayers(). You can also use ipairs instead of pairs as you are iterating over an array.

1 Like

How would I go about connecting the client to the server to the client?

Localscript

local PlayLocalSoundRemote = game.ReplicatedStorage:WaitForChild("PlayLocalSoundRemote")
PlayLocalSoundRemote.OnClientEvent:Connect(function(SoundId, Volume)
	if Volume == nil then Volume = 1 end
	local Sound = Instance.new("Sound", workspace.CurrentCamera)
	Sound.SoundId = SoundId
	if not Sound.IsLoaded then
		Sound.Loaded:Wait()
	end
	Sound.Volume = Volume
	Sound:Play()
	game:GetService("Debris"):AddItem(Sound, Sound.TimeLength)
end)

ServerScript

local PlayLocalSoundRemote = Instance.new("RemoteEvent", game.ReplicatedStorage)
PlayLocalSoundRemote.Name = "PlayLocalSoundRemote"


function PlaySoundForPlayersWithGui(GuiName, SoundId)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.PlayerGui:FindFirstChild(GuiName) ~= nil then
			PlayLocalSoundRemote:FireClient(v, SoundId)
		end
	end
end


--PlaySoundForPlayersWithGui("Radio", "rbxassetid://1837324424")
6 Likes

Thanks so much! You’re a life saver!

1 Like