Making a game and I'm putting in a button as an easter egg. How do I make it so when the button is clicked, a sound plays ONLY for the person that clicked it?
You should use localscript to play the sound instead of a script. Please note a localscript will only play if it a descendant of: A Player’s Backpack A Player’s character model A Player’s PlayerGui A Player’s PlayerScripts. The ReplicatedFirst service
In order to play a sound for a single player, you need to play said sound in a local script. I would go about this by inserting a local script inside of StarterPlayerScripts and a remote event named "PlaySound" in ReplicatedStorage. The local script should contain the text below:
game.ReplicatedStorage.PlaySound.OnClientEvent:Connect(function() sound:Play() end)
Now just insert a server script inside of the click detector that contains the text below:
script.Parent.MouseClick:Connect(function(plr) game.ReplicatedStorage.PlaySound:FireClient(plr) end)
Everything should work now. If you have any questions feel free to leave a comment and don't forget to accept if my answer worked for you.
Explanation
Local scripts only run on clients
not servers but can be exploited, while scripts
run on server and cannot be easily exploited than local scripts
.
A local script
will only work if it's a descendant of the following;
A Player’s Backpack
A Player’s character model
A Player’s PlayerGui
A Player’s PlayerScripts.
The Replicated First Service
Note that you can use local scripts for opening and closing textbuttons, firing remote events and invoking remote functions etc.
You cannot Invoke a Remote function
or fire a Remote Event
by a normal script as normal scripts run on servers, not clients.
Solution
Assuming that the sound is inside the Workspace
, the solution will be as follows;
First of all, insert a local script
into that button and the local script
would be as follows;
script.Parent.MouseButton1Click:Connect(function() -- Clicked function game.Workspace.Sound:Play() -- Change Workspace to wherever your sound is stored print("Clicked!") -- You can remove this piece of line if you want to, it just prints Clicked so that you know a local player has clicked it. end)
Example 2
Let's say your Sound is stored in ReplicatedStorage
and your button is in a SurfaceGui
, you'll have to make sure if the SurfaceGui
is in a part, you'll have to make sure that the Adornee
of the Surface Gui
is the part and AlwaysOnTop
is checked and the Part is inside the StarterGui
. The script would be the same but a little different and sorry if this example sounds confusing..
script.Parent.MouseButton1Click:Connect(function() -- Clicked function game.ReplicatedStorage.Sound:Play() -- Places the sound in the ReplicatedStorage print("Clicked") -- Remove this line if you want to end)
The local script
will play a sound when 1 player has clicked the button, it will not play the sound for the whole server as I've said before, local scripts
only run on clients.
Please make sure to up vote and select this as your answer if it helps you!
Please do not use Scripting Helpers as a script request site. This website is used only to help people with their scripts and not creating scripts for people. Please refer to the Roblox DevForum or look your issue up if you don't know how to do something.