Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sound created by a script HELP?

Asked by 8 years ago

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)
0
Is FilteringEnabled set to true? BlackJPI 2658 — 8y
0
It is not. alexduskthorn 40 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

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

Ad
Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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)
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. alexduskthorn 40 — 8y
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 alexduskthorn 40 — 8y

Answer this question