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

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

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

2 comments

0

I don't understand it

3 answers

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)

5 comments

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?

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.

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.

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. :(

0

Use a different one than, nothing I can really help you with :(

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

1 comment

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.

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

3 comments

0

It's very possible.

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.

0

That is testing whether the GUI died, not the player.

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