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

How do I Make Sounds Play ONLY to the Nearest Player?

Asked by
Wyqkrn 9
5 years ago

Now, I have a little gun script, and this is what I use to play the sound:

pewsound = script:FindFirstChild("Fire")
if pewsound then 
pewsound:Play()

It works, but it plays the sound to every player in the game. I was wondering if there was a way to make the sound play to the player that has the gun equipped? picture The script itself is hosted directly in the item, if that helps at all.

1 answer

Log in to vote
0
Answered by
BuDeep 214 Moderation Voter
5 years ago

I'd personally use a remote event inside of Replicated Storage, and invoke it using a server-script inside of the players tool.

When the player clicks, it fires the event to the players client and a LocalScript inside of the client listens for this and plays the sound.

Here's some example code:

--Server Script inside of the gun:

local storage = game.ReplicatedStorage
local remoteEvent = storage:WaitForChild('Event_Name')

mouseclicked:connect(function()
    remoteEvent:FireClient(player)
end)

--Local Script inside of PlayerGui

remoteEvent.OnClientEvent:connect(function()
    sound:Play()
end)

Now in order to prevent latency issues, I'd only fire it with either a debounce for semi-autos or firing it once whenever a player holds down the mouse and then re-firing it when the player releases the button.

Some links and references that would be helpful:

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

https://developer.roblox.com/en-us/api-reference/class/LocalScript

https://developer.roblox.com/en-us/api-reference/class/Script

Ad

Answer this question