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

Sound created by a script HELP?

Basically, this script used to work. Basically, when you click a GUI, it enables the sound (AND OTHERS COULD HEAR IT). When you click the GUI again (turn it off), it stops.

Now, it only plays the sound for you and no one else can hear it. This is vital for the system I'm using it for. If someone could help, that'd be fantastic! (this is a local script, located inside of a GUI "text" button) If anyone has any ideas at all, please let me know?

local active = false

function click()
	if active == false then
		sound = Instance.new("Sound", game.Players.LocalPlayer.Character.Torso)
		sound.SoundId = "http://www.roblox.com/asset/?id=147605033"
		sound.Volume = 1
		sound.Looped = true
		sound:Play()
		script.Parent.Selected = true
		active = true
	elseif active == true then
		sound:Destroy()
		script.Parent.Selected = false
		active = false
	end
end

script.Parent.MouseButton1Down:connect(click)

2 comments

0

Is FilteringEnabled set to true?

0

It is not.

2 answers

That is because you are using LocalPlayer to parent the sound to the character. If you want everyone in the server to hear it use a ServerScript and also parent it to the workspace not the character. Don't use LocalScript use ServerScript.

I hope I helped and thank you for reading this answer.

~~ KingLoneCat

It turns out that the Play method will only play the sound on that client if used in a local script. However we can fix this by using a RemoteEvent to tell the server to play the sound for us:

Server Script

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:connect(function(player, sound)
	sound:Play()
end

Local Script

local remoteEvent = game.ReplicatedStorage.RemoteEvent
local sound
local active = false

function click()
    if active == false then
        sound = Instance.new("Sound", game.Players.LocalPlayer.Character.Torso)
        sound.SoundId = "http://www.roblox.com/asset/?id=147605033"
        sound.Volume = 1
        sound.Looped = true
        remoteEvent:FireServer(sound)
        script.Parent.Selected = true
        active = true
    elseif active == true then
        sound:Destroy()
        script.Parent.Selected = false
        active = false
    end
end

script.Parent.MouseButton1Down:connect(click)

2 comments

0

Something I've run into is that it works perfect. Except after you disable the sound by clicking on the GUI again, if you try to turn it back on no sound plays.

0

Just an update, it was working, but now when I select the GUI, no sound plays at all. Also, is the "Server Script" section supposed to go somewhere or is that just a reference? Thanks

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