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

How do i play a sound to a certain player instead of everyone in the game?

Asked by
Mayk728 855 Moderation Voter
7 years ago
Edited 7 years ago

So i have a script that plays a sound every time a player dies. I want to make it so only that player can hear the sound instead of everyone in the server. Plz help?

There are 2 scripts that come with it:

1st:

game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() script.Parent.DeathSong:Play() wait(5.4) script.Parent.DeathSong:Stop() end) end) end)

2nd:

script.Parent.DeathSong.SoundId = script.Parent.Song.Value

3 answers

Log in to vote
1
Answered by 7 years ago

You would need to play the music locally, so insert that sound into the PlayerGui.

So, have a LocalScript that controls the sounds it makes [put in the StarterGui], and either insert a sound into the PlayerGui or have a sound in the StarterGui already, both should work fine.

How to add a sound to the playergui:

game.Players.PlayerAdded:connect(function(plr)
    local sound = Instance.new("Sound", plr.PlayerGui) -- parents the sound in there.
end)
0
I just have one problem, every time i place the sound into StarterGui and test it on studio, it works fine. But when i test it on the actual roblox game, it doesn't play that sound. Any idea why? Mayk728 855 — 7y
0
Not without an error. Press f9 in a game to look at the logs. If you own the game, you can also look at the server logs. Fetch the error for help. Without the error my best guess would be you're not waiting for something to load. So use WaitForChild. User#11440 120 — 7y
0
Well, sometimes games need time to load the sound. Is it possible you are not letting it load? Hit play and then wait 10 seconds to be for sure if it isn't working. xXLegendarySoldierXx 129 — 7y
0
I have waited longer than 10 seconds but it still doesn't wanna play the sound on the normal roblox game. I have tried it on multiple devices but still no luck. :( Mayk728 855 — 7y
0
Use a different one than, nothing I can really help you with :( xXLegendarySoldierXx 129 — 7y
Ad
Log in to vote
-2
Answered by 7 years ago

As we know, anything that is in the StartGui will be a local item. Meaning, the sound will only play for that local player. To do this, place an audio inside of startgui.

for i,v in pairs (game.Players:GetPlayers() do
wait(.5)
i.PlayerGui.Sound:Play()
end
0
i is the index, you're missing parenthesis, and the wait is pointless. Also, this method is poor and doesn't really answer the question. User#11440 120 — 7y
Log in to vote
-5
Answered by 7 years ago
local function PlaySound()
    [Where the sound is]:Play()
end

Make sure you have this, but I don't think it is currently possible, not the way I use it for anyway, if you want to make it locally then you could put the sound in a brick, put a minimum and a maximum sound on the sound itself, and when you want to play it locate that brick where the character is. If it doesn't work, please respond back to show me what you have done

0
It's very possible. User#11440 120 — 7y
0
Well, i wanted it to be so that the sound would play if the player dies, not if the player touches a brick. I tried placing the script in workspace and put the sound in StarterGui but could seem to make it work. Tried: script.parent.StarterGui.Dead:Play() and game.workspace.StarterGui.Dead:Play(). I'm not that good of a scripter so i probably made a mistake. Mayk728 855 — 7y
0
That is testing whether the GUI died, not the player. fabiotheone00 -2 — 6y

Answer this question